Advertisement
Shailrshah

Reverse an integer

May 26th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. int back[20];
  4. int reverse(int number, int digits)
  5. {
  6.     int remainder = number%10;
  7.     back[digits++] = remainder;
  8.     number /= 10;
  9.     if(number) reverse(number, digits);
  10.     else{
  11.         int j = 0, total = 0;
  12.         for(int i=0; i < digits; i++)
  13.             total += back[digits-1-i] * pow(10,j++);
  14.         return total;
  15.     }
  16. }
  17. int main(){
  18.     int number;
  19.     printf("Enter the number you want to reverse: ");
  20.     scanf("%d",&number);
  21.     printf("The reverse of %d is %d", number, reverse(number, 0));
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement