Introduction
Reversing a number involves reversing the order of its digits. This guide will show you how to write a C program to reverse a number provided by the user.
Problem Statement
Create a C program that:
- Takes an integer input from the user.
- Reverses the digits of the number.
- Displays the reversed number.
Example:
- Input:
1234 - Output:
4321
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the original number, the reversed number, and the remainder.
- Input the Number: Use
scanfto take input from the user for the number. - Reverse the Number: Use a loop to reverse the digits of the number.
- Display the Reversed Number: Use
printfto display the reversed number.
C Program
#include <stdio.h>
/**
* C Program to Reverse a Number
* Author: https://www.javaguides.net/
*/
int main() {
// Step 1: Declare variables to hold the original number, reversed number, and remainder
int num, reverse = 0, remainder;
// Step 2: Prompt the user to enter a number
printf("Enter an integer: ");
scanf("%d", &num);
// Step 3: Reverse the number using a loop
while (num != 0) {
remainder = num % 10; // Step 3.1: Get the last digit
reverse = reverse * 10 + remainder; // Step 3.2: Append the last digit to the reversed number
num = num / 10; // Step 3.3: Remove the last digit from the original number
}
// Step 4: Display the reversed number
printf("Reversed number: %d\n", reverse);
return 0; // Step 5: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- The variable
numis declared to store the original number entered by the user.reverseis initialized to 0 and will hold the reversed number.remainderis used to store the last digit of the number during each iteration of the loop.
Step 2: Input the Number
- The program prompts the user to enter an integer using
printf. Thescanffunction then reads the input and stores it in the variablenum.
Step 3: Reverse the Number Using a Loop
- The program uses a
whileloop to reverse the digits of the number:- Step 3.1: The remainder when
numis divided by 10 gives the last digit ofnum. - Step 3.2: This last digit is then added to the
reversevariable after multiplyingreverseby 10 to shift its digits to the left. - Step 3.3: The original number
numis divided by 10 to remove its last digit. This process repeats untilnumbecomes 0.
- Step 3.1: The remainder when
Step 4: Display the Reversed Number
- The program displays the reversed number using the
printffunction.
Step 5: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example 1:
Enter an integer: 1234
Reversed number: 4321
Example 2:
Enter an integer: 98765
Reversed number: 56789
Example 3 (Single Digit):
Enter an integer: 7
Reversed number: 7
Conclusion
This C program demonstrates how to reverse the digits of a number entered by the user. It covers basic concepts such as loops, arithmetic operations, and handling user input, making it a useful example for beginners learning C programming.