Program for Reverse Number in C or C++

Program for Reverse Number in C or C++

Reverse number logic is same as Armstrong number. Reversing a number means we simply bring the last digit first and first digit as last. Here we will make use of while loop. Let’s see the program for reverse number in C or C++.

1. Code

C program

// C program to reverse a number
#include<stdio.h>  

 int main()    
{    
    int num, rem, rev = 0, temp;    
    printf("\nEnter the number : ");    
    scanf("%d",&num);    
    temp = num;    
    while(num>0)    
    {    
        rem = num%10;    
        rev = rev*10 + rem;    
        num = num/10;    
    }    
      
    printf("\nReverse of %d is : %d",temp,rev);        
        
    return 0;  
} 

C++ program

#include <iostream>

using namespace std;

int main() {
    int num, temp, rem, rev = 0;
    cout << "\nEnter a number : ";
    cin >> num;
    temp = num;

    while (num != 0) 
    {
        rem = num % 10;       
        rev = rev*10 + rem; 
        num = num/10;
    }

    cout << "Reverse of "<<temp<< " is : "<<rev;

    return 0;
}

2. Output

program for reverse number in C or C++

Also Read : Algorithm And Program for Armstrong Number.

3. Program Parts

  1. #include is a pre-processor directive. It is used to include header files.
  2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
  3. .h is an extension for header file.
  4. int is data type. It indicates that the program returns a value.
  5. main() is main function that indicates the compiler that the user written programs starts from here.
  6. num, rem, sum, temp are variable names of integer data type.
  7. while is looping structure in C/C++. It repeats the code inside it untill a specific condition is satisfied.
  8. print/cout is the keyword use to print a message.
  9. Scanf/cin is the keyword use to scan a value entered by user.
  10. return statement is use to return a value. If any error occurs the program will return 0.

4. Explanation

The program will accept a number. Then it will store the accepted number in temp variable. Now the program will enter while loop. In while loop it will divide the accepted number by 10 and the remainder will be stored in rem. rev is used to store the reverse number. Then the num will divide itself with 10 and store the quotient in num only. The loop will execute until the condition is satisfied. Once the condition is not satisfied it will come out of the loop. At last it will print the reverse of entered number.

Conclusion

So this was the program for reverse number in C or C++. There are many other possible ways to find reverse number. If you know any of them do tell us in the comment section below. Hope you liked it. If yes then please do share it with your friends and programming buddies. Also tell us what other programs you would like us to post.

For more such content follow us on instagram. Visit again!

ErrorFreeProgram.

4 COMMENTS

comments user
안전토토

This is something New !

    comments user
    ErrorFreeProgram

    Thank you!🙌

comments user
https://j-website.net

This is something New !

    comments user
    ErrorFreeProgram

    Thank You!

Leave a Reply

Your email address will not be published. Required fields are marked *