Answer to Question #203753 in C++ for she

Question #203753

Write a C++ program to print the digits of a number in reverse order. The user will enter the number and your program will display in reverse order. Make sure user only enters a positive number, if negative number, display an error message.


1
Expert's answer
2021-06-05T23:56:56-0400


#include <iostream>
using namespace std;

int main() {
    int n, reversedNumber = 0, remainder;

    cout << "Enter an integer: ";
    cin >> n;

    while(n != 0) {
        remainder = n%10;
        reversedNumber = reversedNumber*10 + remainder;
        n /= 10;
    }

    cout << "Reversed Number = " << reversedNumber;

    return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog