C Program to Reverse a Number – Simple Logic with Code

Introduction

Reversing digits of a number is one of the most common exercises for beginners in C programming. This guide will help you understand how to write a C program to reverse a number using a while loop. It is a useful problem to develop logic-building skills and understand how integer manipulation works in the C language.


What Does It Mean to Reverse a Number in C?

To reverse a number means to flip its digits in reverse order. For example, reversing 1234 will result in 4321. This is done by extracting each digit from the right and rebuilding the number from left to right. A while loop along with modulus and division operators makes this task simple in C.


C Program to Reverse a Number

#include <stdio.h>

int main() {
int number, reversed = 0, remainder;

// Input from user
printf("Enter an integer: ");
scanf("%d", &number);

// Reverse logic
while (number != 0) {
remainder = number % 10;
reversed = reversed * 10 + remainder;
number /= 10;
}

// Output reversed number
printf("Reversed number is: %d\n", reversed);

return 0;
}

Explanation of the C Program to Reverse a Number

This C program to reverse a number works by using the following logic:

  1. Take an integer input from the user.
  2. Extract the last digit using the modulus operator (% 10).
  3. Add the digit to the reversed number after shifting the existing digits left (reversed * 10 + remainder).
  4. Remove the last digit from the original number using integer division (/ 10).
  5. Repeat the steps until the original number becomes zero.

Sample Output

Enter an integer: 785
Reversed number is: 587

Enter an integer: 1002
Reversed number is: 2001

Practical Use Cases

This program can be helpful for:

  • Checking palindrome numbers
  • Reordering digits in number-based puzzles
  • Practicing loop and condition logic in C programming

Conclusion

This blog demonstrated how to write a C program to reverse a number using a simple and clean approach. This type of logic-building exercise strengthens understanding of loops, conditions, and integer operations in C. It is a foundational program every beginner should practice.

Previous Article

C Program to Calculate Factorial of a Number

Next Article

C Program to Generate First N Numbers of Fibonacci Series

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨