Updated March 17, 2023
Introduction to Reverse Number in C
There are many programming languages, and C language is known for base language for all programming language. It allows the user to do perform various operations using inbuilt functions. Other than inbuilt, it also allows us to create customize functions to develop a new logic. In this article, we are going to see how we can reverse a number in C language. Reversing number in C means printing the given number back to the front. For example, the given number is 123, then the reverse of this number is 321. Let us see how we can build this logic in the C program.
Logic of Reverse Number in C
To reverse a number in C, we used modulus (%). The logic for the reverse number is as follows:
- First, initialize a reverse number to 0.
- Then, multiply reverse by 10.
- Divide given number by 10 and find modulus.
- Add the modulus and reverse number.
- Print the result of 4th step.
- Divide a given number by 10
- Repeat the step from 2 to 6 until the output comes.
How to Reverse a Number in C?
In this section, we are going to see how to reverse a number using various methods with the help of examples.
Example 1: Reverse Number Using the While Loop
First, we have to understand the working of the While loop. While the loop gets executed several times based on the condition which is mentioned after the word While in code. If this condition is true then it will execute the code inside the parenthesis of the While loop. If the condition is false then it will jump to the code after the While loop without executing the code of While loop. Let’s see how to find reverse numbers using the While loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int number, reverse_number = 0;
printf("Enter a number to reverse value:"); // allow user to enter a number
scanf("%d", &number); // takes value from user
while (number != 0)
{
reverse_number = reverse_number * 10;
reverse_number = reverse_number + number % 10;
number = number / 10;
}
printf("Reverse of entered number is: %d", reverse_number); //print reverse value
return 0;
}
Output:
Example 2: Reverse Number Using Recursion
Code:
#include<stdio.h>
#include<conio.h>
int reverse(int);
int main()
{
int number, reverse_number = 0;
printf("Enter a number to reverse value:"); // allow user to enter a number
scanf("%d", &number); // takes value from user
reverse_number = reverse(number);
printf("Reverse of entered number is: %d", reverse_number); // print reverse value
return 0;
}
reverse(int number)
{
static reverse_number = 0;
if (number == 0)
return 0;
reverse_number = reverse_number * 10;
reverse_number = reverse_number + number % 10;
reverse(number/10);
return reverse_number;
}
Output:
Example 3: Reverse Number Using For Loop
First, we will understand the working of For Loop which is given below:
In the For loop, firstly we initialize and declare variables for the code. After that condition is evaluated. This initialization step is executed only once in the Code. If the condition is true then it will execute the code inside the block of For loop. If the condition is false then it will jump to the code after the For loop without executing the code of For loop. After the For loop, the increment statement will be executed. After that again the condition will be checked. Loop will get executed if the condition is true and the loop will repeat itself i.e. the body of the loop, an increment statement, and condition. The loop ends when the condition is false. Let’s see how to find a reverse number using For loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int number, remainder_number, reverse_number= 0, i;
printf("Enter a number to reverse value:"); // allow user to enter a number
scanf("%d", &number); // takes value from user
for(i = number; i >0; )
{
remainder_number= i % 10;
reverse_number = remainder_number + reverse_number * 10;
i = i/ 10;
}
printf("Reverse of entered number is: %d", reverse_number); //print reverse value
return 0;
}
Output:
Example 4: Reverse Number Using the Do-while Loop
A do-while loop is similar to a while loop, But in the do-while loop, the loop gets executed at least one time.
In the Do While loop, The condition appears at the end of the loop, so the statements in the Do loop executed before checking whether the condition is true or false. If the condition is true, the loop will go back up to do, and the statements in the loop will get executed again. This process repeats again and again and terminates when the given condition becomes false. Let’s see how to find reverse numbers using Do While loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int number, reverse_number = 0;
printf("Enter a number to reverse value:"); // allow user to enter a number
scanf("%d", &number); // takes value from user
do
{
reverse_number = reverse_number * 10;
reverse_number = reverse_number + number % 10;
number = number / 10;
}while(number != 0);
printf("Reverse of entered number is: %d", reverse_number); //print reverse value
return 0;
}
Output:
Conclusion
In this article, we have seen how to reverse a number in C with some examples along with elaborated examples. I hope this article will help you in understanding the working of factorial in C and you find this article helpful.
Recommended Articles
This is a guide to Reverse Number in C. Here we discuss how to Reverse a Number in C along with logic and examples with code implementation. You can also go through our other suggested articles to learn more –