- 1C Hello World Program
- 2C Program to Print Your Own Name
- 3C Program to Print an Integer Entered by the User
- 4C Program to Add Two Numbers
- 5C Program to Multiply Two Floating‑Point Numbers
- 6C Program to Print the ASCII Value of a Character
- 7C Program to Swap Two Numbers
- 8C Program to Calculate Fahrenheit to Celsius
- 9C Program to Find the Size of int, float, double and char
- 10C Program - Add Two Complex Numbers
- 11C Program - Find Simple Interest
- 12C Program - Find Compound Interest
- 13C Program - Area And Perimeter Of Rectangle
- 14C Program - Check Whether a Number is Positive, Negative, or Zero
- 15C Program - Check Whether Number is Even or Odd
- 16C Program - Check Whether a Character is Vowel or Consonant
- 17C Program - Find Largest Number Among Three Numbers
- 18C Program - Calculate Sum of Natural Numbers
- 19C Program - Print Alphabets From A to Z Using Loop
- 20C Program - Make a Simple Calculator
- 21C Program - Generate Multiplication Table
- 22C Program - Reverse a Number
- 23C Program - Check whether the input number is a Neon Number
- 24C Program - Find All Factors of a Natural Number
C Program – Reverse a Number - Digits Reversal Explained
Reverse a Number
This program takes an integer input from the user and reverses its digits. For example, input 12345 yields output 54321. We accomplish this by repeatedly taking the last digit from the original number and appending it to a new reversed number.
#include <stdio.h>
int main() {
int n, rev = 0, rem; // n is the input, rev holds the reversed number, rem stores each digit
printf("Enter an integer: ");
scanf("%d", &n);
// Loop until all digits are processed
while (n != 0) {
rem = n % 10; // Take the last digit
rev = rev * 10 + rem; // Append it to the reversed number
n /= 10; // Remove the last digit from n
}
printf("Reversed number = %d\n", rev);
return 0;
}
Enter an integer: 12345
Reversed number = 54321
The key idea is to repeatedly extract the last digit of the number using the modulus operator (%). Each extracted digit is appended to the reversed number by multiplying the current reversed value by 10 (to make space for the new digit) and adding the digit. After processing, the original number becomes zero and the reversed number holds the digits in the opposite order.
⬅ Previous TopicC Program - Generate Multiplication Table
Next Topic ⮕C Program - Check whether the input number is a Neon Number
Next Topic ⮕C Program - Check whether the input number is a Neon Number
Comments
Loading comments...