Reverse number program in C using for loop
Last updated:12th Aug 2022
In this example, you will write a c program to find the reverse of a number entered by the user by using for loop.
for example, if the user input is 123 then the reverse of 123 is 321.
#include <stdio.h>
int main() {
int n, reverse = 0, remainder;
printf("Enter an number: ");
scanf("%d", &n);
for( ; n!=0; n /= 10){
remainder = n % 10;
reverse = reverse * 10 + remainder;
}
printf("Reversed of number is: %d", reverse);
return 0;
}
output
Enter an number: 2468 Reversed of number is: 8642
explanation of a reverse number
- Take a number from the user and store it in variable
n
. - Iterate for loop from
n
ton!=0
. - Find the remainder of a number n%10 and add the remainder into the variable
reversed
. - Divide n by 10 i.e
n/=10
. perform steps 2 to 5 until n becomes 0.