C program to find reverse of a number
Learn how to write a C program to find the reverse of a number. This article provides a detailed explanation and sample code for this fundamental task in C programming.
Reversing the digits of a number is a fundamental problem in programming that helps in understanding basic arithmetic operations and loops. This article will guide you through writing a C program to reverse the digits of a given number, providing a detailed explanation and sample code.
Steps to Find the Reverse of a Number
To solve this problem, follow these steps:
- Input the Number: Read the number from the user.
- Extract Each Digit: Use a loop to extract each digit of the number.
- Construct the Reverse: Build the reversed number by appending the extracted digits.
- Print the Result: Output the reversed number to the console.
Write a C program to find reverse of a number
Here's a C program to reverse the digits of a number:
#include <stdio.h>
int main() {
int number, digit, reverse = 0;
// Input the number
printf("Enter an integer: ");
scanf("%d", &number);
// Calculate the reverse of the number
while (number != 0) {
digit = number % 10; // Extract the last digit
reverse = reverse * 10 + digit; // Append the digit to the reversed number
number /= 10; // Remove the last digit
}
// Print the result
printf("Reversed number: %d", reverse);
return 0;
}
Output
Enter an integer: 1234
Reversed number: 4321
Explanation
- Input the Number: The program prompts the user to enter an integer. The
scanf
function reads the input number and stores it in the variablenumber
. - Extract Each Digit:
- The
while
loop continues as long asnumber
is not equal to0
. - The last digit is extracted using the modulus operator (
%
). For example, ifnumber
is1234
,digit = 1234 % 10
will result indigit = 4
.
- The
- Construct the Reverse:
- The extracted digit is appended to the
reverse
variable by multiplyingreverse
by 10 and adding the digit. For the initial iteration,reverse = 0 * 10 + 4
results inreverse = 4
. - The last digit is removed from
number
using integer division (/
). For example,number = 1234 / 10
results innumber = 123
.
- The extracted digit is appended to the
- Repeat: The loop continues, extracting and appending each digit until
number
is0
. - Print the Result: The program prints the reversed number to the console using the
printf
function.
Detailed Steps
- Step 1: Input the Number
- Use
printf
to prompt the user for input. - Use
scanf
to read the number from the user.
- Use
- Step 2: Extract Each Digit
- Use a
while
loop to continue processing as long asnumber
is not zero. - Use the modulus operator (
%
) to get the last digit of the number.
- Use a
- Step 3: Construct the Reverse
- Multiply the
reverse
variable by 10 and add the extracted digit. - Remove the last digit from the number using integer division (
/
).
- Multiply the
- Step 4: Print the Result
- Use
printf
to display the reversed number.
- Use
Example
For an input of 1234
, the program will:
- Extract
4
and append it toreverse
(reverse = 4). - Extract
3
and append it toreverse
(reverse = 43). - Extract
2
and append it toreverse
(reverse = 432). - Extract
1
and append it toreverse
(reverse = 4321).
The output will be Reversed number: 4321
.