How to reverse a number in c++? Detailed guide with sample program

Reversing a number in C++ can be accomplished in a few different ways. One method is to convert the number to a string, reverse the string, and then convert it back to an integer. Another method is to use mathematical operations to reverse the digits of the number.

In this blog post, we will go over both of these methods and explain how they work.

How to reverse a number in c++?

how to reverse a number in c++
How to reverse a number in c++?

Method 1: Using String Conversion

One way to reverse a number in C++ is to convert it to a string, reverse the string, and then convert it back to an integer. Here is the code to accomplish this:

program
include
include
int main() {
int num = 12345;
std::string numString = std::to_string(num);
std::reverse(numString.begin(), numString.end());
int reversedNum = std::stoi(numString);
std::cout << reversedNum << std::endl;
return 0;
}
output
54321

In this code, we start by initializing an integer variable num with the value 12345. Next, we use the std::to_string() function to convert the integer to a string. We then use the std::reverse() function to reverse the string, which reverses the order of the digits. Finally, we use the std::stoi() function to convert the reversed string back to an integer. The reversed number is then output to the console.


You might be interested:


Method 2: Using Mathematical Operations

Another way to reverse a number in C++ is to use mathematical operations. This method does not involve converting the number to a string, which can be faster and more memory efficient. Here is the code to accomplish this:

program
include
int main() {
int num = 12345;
int reversedNum = 0;
while (num > 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
std::cout << reversedNum << std::endl;
return 0;
}
output
54321

In this code, we start by initializing an integer variable num with the value 12345. Next, we initialize an integer variable reversedNum to store the reversed number. We then use a while loop to iterate through each digit of the number. Inside the loop, we use the modulus operator % to get the last digit of the number, and add it to the reversed number.

We also multiply the reversed number by 10 to make room for the next digit. We then use the division operator / to remove the last digit from the original number, so that we can continue processing the remaining digits. Finally, the reversed number is output to the console.

Both methods will give the same output 54321

Conclusion

In conclusion, there are several ways to reverse a number in C++, including using string conversion and mathematical operations. The method you choose will depend on your specific needs and constraints, such as performance and memory usage. Both methods explained above will give the same result, but have different performance and memory usage characteristics.


You might be interested:


Tags:

Have any doubt or suggestion? feel free to write here :)

Leave a reply

ProProfession.com
Logo