Reversing a number can be done in 3 simple steps.
1. Find the remainder using %
, you will get the last digit of a number, r = num % 10
.
2. rev = rev * 10 + r
will append r
to rev.
After all the iterations, the final reverse number will be saved in rev
.
3. Find the remaining number using num = num / 10
//program name: Reverse_of_Number.c
#include<stdio.h>
int main()
{
int num,r,rev=0;
printf("Enter a number to find reverse: ");
scanf("%d",&num);
while(num)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("Reversed of given number: %d",rev);
return 0;
}
OUTPUT:
Enter a number to find reverse: 1234 Reversed of given number: 4321
The following codes help you to understand more about Reversing numbers, String and Array elements.
C Program to Find Reverse of a Number using Recursion
Flowchart for Reverse of a Number
Java Program To Read Reverse and Print a String in Alphabetical Order