/*program to output maximal palindrom of a number *uses a recursive dunction "reverse" * * Author:rahule@cse.iitk.ac.in */ #include int reverse(int); main() { int n,pal,t,p; printf("\nEnter the number :"); scanf("%d",&n); while(n) //run this loop till n becomes 0 or till a maximal palindrom is found { if(n==reverse(n)) //if n = reverse(n),then n is the maximal palindrom..so break break; t=n;p=1; //follwing code strikes off first and last digit of the number while(t) //calulates p such that p is 1 followed by x zeros,where x is the number of digits in 'n' { p=p*10; t=t/10; } p=p/10; //strikes out last 0 in p n=n%p; //strikes out first digit in n n=n/10; //strikes out last digit in n } printf("\nthe maximal palindrom :%d",n); } int reverse(int n) //function to reverse n { int p=1,t,d,temp; if(n/10==0) //if n has only one digit,return that return(n); else { p=1; t=n; while(t) { p=p*10; t=t/10; } p=p/10; d=n/p; //takes the first digit of n in d return(reverse(n%p)*10+d); //reverses therest of the digits and appends d to it } }