Tuesday, 17 March 2015

C program to reverse number using recursion

#include <stdio.h>
 
long reverse(long); 
 
int main()
{
   long n, r;
 
   scanf("%ld", &n);
 
   r = reverse(n);
 
   printf("After reverse the number %ld\n", r);
 
   return 0;
}
 
long reverse(long n) {
   static long r = 0;
 
   if (n == 0) 
      return 0;
 
   r = r * 10;
   r = r + n % 10;
   reverse(n/10);
   return r;
} 
 
 
Output Of Program:
1234

After reverse the number 4321
 

No comments:

Post a Comment