Updated March 17, 2023
Overview of Reverse Number in C++
In this article, we are going to see how we can reverse a number in the C++ language. it means printing the given number back to the front. For example, the given number is 864, then the reverse of this number will be 468. Let us see how we can build this logic in C++ with some examples. We can reverse the numbers or characters using various loops or combinations of loops as well as arithmetic operators.
Logic of Reverse Number in C++
Modulus (%) sign is used to find the reverse of the given number. The logic for a reverse number is as follows:
- Initialize a reverse number to 0.
- Multiply the reverse number 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 The C++ Language?
In this section, we are going to see how to reverse a number using various methods like while loop, recursion, for loop and do while loop with the help of examples.
Example 1: Find Reverse Number in C++ Using While Loop
Before moving to the program, let’s first understand how while loop works. While the loop gets executed several times until the condition matched. The condition will differ based on the requirement of the program. While loop first checks the condition, and then start the loop. If the given condition is true then it will transfer the flow control to the statements defined in the while loop. If the given condition is false then it will transfer the flow control to the statements that are defined outside the while loop. i.e. in this program, while loop will continue till the number != 0 is false. It will skip the statement defined inside the while loop. Let’s see how to find a reverse number in C++ using the While loop.
Code:
#include <iostream>
using namespace std;
int main()
{
int number, reverse_number = 0;
cout << "Enter a number to reverse value:"; // allow user to enter a number
cin >> number; // takes value from user
while (number != 0)
{
reverse_number = reverse_number * 10;
reverse_number = reverse_number + number % 10;
number = number / 10;
}
cout << "Reverse of entered number is:";
cout << reverse_number; //print reverse value
return 0;
}
Output:
Example 2: Find Reverse Number in C++ Using Recursion
Code:
#include <iostream>
using namespace std;
int reverse(int);
int main()
{
int number, reverse_number = 0;
cout << "Enter a number to reverse value:"; // allow user to enter a number
cin >> number; // takes value from user
reverse_number = reverse(number);
cout << "Reverse of entered number is: ";
cout << reverse_number; // print reverse value
return 0;
}
int reverse(int number)
{
static int 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: Find Reverse Number in C++ Using for Loop
Before moving to the program, lets first understand how a loop works. In the loop, firstly we initialize a variable for the code. After the variable is initialized we mentioned some conditions to evaluate a program. This initialization step is executed only once in the Code. And after that, the increment or decrement need to be declared. It also depends on the requirement of the program.
For loop executes until the given condition meets. If the condition is true then it will transfer the flow control to the statements inside the block of for loop. If the condition is false then it will transfer the flow control to the statements that are outside the block of for loop and skip the for a loop.
Code:
#include <iostream>
using namespace std;
int main()
{
int number, remainder_number, reverse_number= 0, i;
cout << "Enter a number to reverse value:"; // allow user to enter a number
cin >> number; // takes value from user
for(i = number; i >0; )
{
remainder_number= i % 10;
reverse_number = remainder_number + reverse_number * 10;
i = i/ 10;
}
cout << "Reverse of entered number is: ";
cout << reverse_number; //print reverse value
return 0;
}
Output:
Example 4: Find Reverse Number Using Do While Loop
A do-while loop is similar to a while loop, the only difference is while loop first checks the condition and then executes the code while do-while loop first executes the code and then check the condition.
As do while loop check condition at the end, it will execute the loop at least one time. In do-while loop, the condition is declared at the end of the loop.
If the given condition is true, the loop will transfer the flow control back to do, and all statements in the loop will get executed again. This process repeats until the given condition match. Let’s see how to find a reverse number in C++ using do-while loop.
Code:
#include <iostream>
using namespace std;
int main()
{
int number, reverse_number = 0;
cout << "Enter a number to reverse value:"; // allow user to enter a number
cin >> number; // takes value from user
do
{
reverse_number = reverse_number * 10;
reverse_number = reverse_number + number % 10;
number = number / 10;
}while(number != 0);
cout << "Reverse of entered number is: ";
cout << reverse_number; //print reverse value
return 0;
}
Output:
Recommended Articles
This is a guide to Reverse Numbers in C++. Here we discuss the overview and how to reverse a number in C++ Language along with different Examples. You may also look at the following articles to learn more –