Calculate reverse of a number

This program reverse the number entered by the user and then prints the reversed number on the screen.  In our program we use modulus(%) operator to obtain the digits of a number. To invert number look at it and write it from opposite direction or the output of code is a number obtained by writing original number from right to left. To reverse or invert large numbers use long data type or long long data type if your compiler supports it, if you still have large numbers then use strings or other data structure.

In the following program we have taken two variables to store two numbers- “n” to take input from user and “rev” to perform calculations.



int main()
   int n, rev = 0;

   printf("Enter a number \n");

   while (n != 0)
      rev = rev * 10; 
      rev = rev + n%10;  
      n = n/10;

   printf("Reverse of entered number is = %d\n", rev);

   return 0;

Enter a number 53
Reverse of the entered number is= 35

Mohit Arora
Follow me