Write a C++ program that takes up to 10-digit integer input from user (can be 1 digit, 2 digit, and so on..); passes this to a function which reverses the digits. Finally, the reversed number should be displayed in the main function. For example: when user enters 10-digit like 1234567890 your function will reverse it to 987654321. Do not use strings. Be careful for big integer values. [use functions, decision control]
#include <iostream>
using namespace std;
int rev(int n){
int remainder, reversed_number = 0;
while(n != 0) {
remainder = n % 10;
reversed_number = reversed_number * 10 + remainder;
n /= 10;
}
return reversed_number;
}
int main() {
int n;
cout << "Enter an integer: ";
cin >> n;
cout << "Reversed Number = " << rev(n);
return 0;
}
Comments
Leave a comment