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:

  1. Scan a number from a user.
  2. Initialize a reverse_number with 0.
  3. Now, one by one fetch out the last digit from a number using modulus operator and concatenate to reverse_number.
  4. 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:

  1. Program to swap two Numbers.
  2. Program to print Fibonacci Series up to Nth Term.
  3. Program to find and print nth Fibonacci number.
  4. Program to find Power of the Number.
  5. Program to find Quotient and Remainder.
  6. Program to find largest amongst three numbers.
  7. Program to find factorial of a number.
  8. Program to find GCD of two numbers.
  9. Program to find LCM of two numbers.
  10. Program to check whether entered number is odd or even.
  11. Program to check whether entered number is prime number or not.
  12. Program to check whether entered number is palindrome or not.
  13. Program to check whether entered number is Armstrong Number or Not.
  14. Program to convert binary number to octal number.
  15. Program to convert binary number to decimal number.
  16. Program to convert binary number to hexadecimal number.
  17. Program to convert octal number to binary number.
  18. Program to convert octal number to decimal number.
  19. Program to convert octal number to hexadecimal number.
  20. Program to convert decimal number to binary number.

You may also like...