C Program to Find Reverse of a Number. In the world of computer programming reversing a number is a classic problem that beginners encounter. In this article, we will see how to write a C program to reverse a number.
Table of Contents
Reverse a Number
This task involves taking an integer as input and producing another integer that is the reverse of the input number. For example, if we have a number 12345 as input then it could be 54321 as output. The process of reversing involves extracting each digit from the original input number and arranging these digits in opposite order.
C Program to Find Reverse of a Number
Let’s write a C program to find the reverse of a number.
#include <stdio.h>
void main()
{
int number, reversenumber = 0, digit;
printf("Enter the number ");
scanf("%d", &number);
while (number != 0)
{
digit = number % 10;
reversenumber = reversenumber * 10 + digit;
number = number / 10;
}
printf("Reverse number = %d", reversenumber);
}
Output
In the above C program-
- We are using a while loop to extract each digit from the number.
- Multiplying the reverse number by 10 and adding the extracted digit.
- Updating the number with number/10.
- Continuing the process until the number becomes 0.
- At last, we are printing the reverse number.
Writing a C program to reverse a number is an excellent way to understand basic programming concepts such as loops, conditional statements, and arithmetic operations.
Happy Coding & Learning
1 thought on “C Program to Find Reverse of a Number”