Extracting digits in C++

0
41

Write a C++ program to extract and print the digits in reverse order of a number in C++.

Source Code:

#include <iostream>
using namespace std;

int main()
{
    int num;
    int a = 0;

    cout << "Enter a number: ";
    cin >> num;
    cout << "\n";
    for (int i = 1; num > 0; i++) 
    {
        a = num % 10;
        cout << a;
        num = num / 10;
    }
    return 0;
}

Output:

Sample # 1
Enter a number: 456
654

Sample # 2
Enter a number: 123
321

LEAVE A REPLY

Please enter your comment!
Please enter your name here