int n, r, l = 0; //n=num, r= reminder , l= largest. // this is the logic code only. while (n>0) { r = n % 10; if(l<r) { l = r; } n = n / 10; }