Reverse a Number
“Reverse a Number” is a very basic programming exercise problem for beginners. Here, we are given a number, entered by user and our task is to write a program to reverse a number.
Example:
INPUT: Num = 45 OUTPUT: Num = 54
The steps required to reverse a number are as follows:
- Scan a number from a user.
- Initialize a reverse_number with 0.
- Now, one by one fetch out the last digit from a number using modulus operator and concatenate to reverse_number.
- Finally, print the reverse number.
C++ Program to reverse a number is as follows:
#include<bits/stdc++.h> using namespace std; int main() { //Scan the number int num; cout<<"Enter the number:"; cin>>num; //Printing the number before reversing cout<<"Number Before Reversing:"<<num<<"\n"; // Reverse the number int reverse = 0; while(num > 0) { int digit = num % 10; reverse = reverse*10 + digit; num = num/10; } num = reverse; // Printing the number after reversing cout<<"Number After Reversing:"<<num<<"\n"; }
OUTPUT: Enter the number: 27 Number Before Reversing: 27 Number After Reversing: 72
Related Posts:
- Program to swap two Numbers.
- Program to print Fibonacci Series up to Nth Term.
- Program to find and print nth Fibonacci number.
- Program to find Power of the Number.
- Program to find Quotient and Remainder.
- Program to find largest amongst three numbers.
- Program to find factorial of a number.
- Program to find GCD of two numbers.
- Program to find LCM of two numbers.
- Program to check whether entered number is odd or even.
- Program to check whether entered number is prime number or not.
- Program to check whether entered number is palindrome or not.
- Program to check whether entered number is Armstrong Number or Not.
- Program to convert binary number to octal number.
- Program to convert binary number to decimal number.
- Program to convert binary number to hexadecimal number.
- Program to convert octal number to binary number.
- Program to convert octal number to decimal number.
- Program to convert octal number to hexadecimal number.
- Program to convert decimal number to binary number.