English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
In this example, you will learn to reverse the number entered by the user.
To understand this example, you should know the followingC programmingTopic:
#include <stdio.h> int main() { int n, rev = 0, remainder; printf("Enter an integer: "); scanf("%d", &n); while (n != 0) { remainder = n %% 10; rev = rev * 10 + remainder; n /= 10; } printf("Reverse number = %d", rev); return 0; }
Output result
Enter an integer: 2345 reverse number =}} 5432
This program takes an integer input from the user. Then it uses while loop until it is false (0) for n != 0.
in each iteration of the loop, the remainder of n when calculated is divided by10and decrease the value n by10times.
Inside the loop, use the following formula to calculate the reciprocal:
rev = rev*10 + remainder;