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.
#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;
}
Comments
Leave a comment