Q:

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

belongs to collection: C++ programs on various topics

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C++ code to extract and print digits of a number in reverse order

#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

Enter a number: 123456

654321

Here we are first using a loop with condition num>0, and the last digit of the number is taken out by using simple % operator after that, the remainder term is subtracted from the num. Then number num is reduced to its 1/10th so that the last digit can be truncated.

 

The cycle repeats and prints the reverse of the number num.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to print all Even and Odd numbers from... >>
<< C++ program to find the power of a number using lo...