C Program to reverse a number entered by user
Write a C program to enter any number from user and find the reverse of given number using for loop. How to find reverse of any number in C programming using loops. Program to find reverse of a given number.
C Program to reverse a number :- This program reverse the number entered by the user and then prints the reversed number on the screen.
Example:
Input number: 1234
Output reverse: 4321
Logic to find reverse :
The process of reversing involves four basic steps:
-
Multiply the rev variable by 10.
-
Find the last digit of the given number.
-
Add the last digit just found to rev.
-
Divide the original number by 10 to eliminate the last digit, which is not needed anymore.
Repeat the above four steps till the original number becomes 0 and finally we will be left with the reversed number in rev variable.
Here is source code of the C program to reverse a number . The C program is successfully compiled and run(Codeblocks) on a Windows system. The program output is also shown below.
SOURCE CODE : :
#include<stdio.h>
#include<math.h>
int main()
{
long int num, rev = 0, dig;
printf("\n\nENTER A NUMBER-----: ");
scanf("%ld", &num);
while(num>0)
{
dig = num % 10;
rev = rev * 10 + dig;
num = num / 10;
}
printf("\nREVERSED NUMBER IS----: %ld", rev);
return 0;
}
OUTPUT : :
ENTER A NUMBER-----: 286734 REVERSED NUMBER IS----: 437682