Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Display the number in reverse order

C++ For Loop: Exercise-30 with Solution

Write a program in C++ to display the number in reverse order.

Pictorial Presentation:

C++ Exercises: Display the number in reverse order

Sample Solution:-

C++ Code :

#include <iostream>
using namespace std;

int main()
{
    int num, r, sum = 0, t;
    cout << "\n\n Display the number in reverse order:\n";
    cout << "-----------------------------------------\n";
    cout << " Input a number: ";
    cin >> num;
    for (t = num; num != 0; num = num / 10) 
    {
        r = num % 10;
        sum = sum * 10 + r;
    }
    cout << " The number in reverse order is : " << sum << endl;
}

Sample Output:

 Display the number in reverse order:                                  
-----------------------------------------                              
 Input a number: 12345                                                 
 The number in reverse order is : 54321  

Flowchart:

Flowchart: Display the number in reverse order

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to find LCM of any two numbers using HCF.
Next: Write a program in C++ to find out the sum of an A.P. series.

What is the difficulty level of this exercise?