In this tutorial you will learn about the C++ Programs to Reverse any Number and its application with practical example.
C++ Programs to Reverse any Number
In this tutorial, we will learn to create a C++ program that will Reverse any Number using C++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ programming.
- For loop in C++ programming.
- Conditional Statements in C++ programming.
- Arithmetic operations in C++ Programming.
Program to Reverse any Number:-
In c++ programming, it is possible to take integer input from the user and Reverse any Number with the help of a C++ program. The reversing of a number means the digits of a number will be swapped from the first position to the last position. The C++ language has many types of header libraries that have supported functions in them with the help of these files the programming is easy.
With the help of this program, we can Reverse any Number.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Calculating the number is a Palindrome number or not. 4. Printing the result. 5. End the program. |
Program to Reverse any Number:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> using namespace std; int main() { //Declaring the variables for the program. int n, revno = 0, rmdr; //Taking integer number in input from the user for reverse that number cout << "Enter an integer: "; cin >> n; //Reversing the number while(n != 0) { rmdr = n%10; revno = revno*10 + rmdr; n /= 10; } //Printing the output number cout << "Reversed Number = " << revno; return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- num= it will hold the integer value of the input.
- n = it will hold the integer value.
- digit = it will hold the integer value.
- rev = it will hold the integer value.
Input message for the user for the integer value.
Program Logic Code.
Printing output Reverse any Number.