Write a C++ Function 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>
#include <cmath>
using namespace std;
long int ReverseDigit(long int n)
{
long int res=0;
int k = n;
int i = -1;
while (k > 0)
{
k /= 10;
i++;
}
while (n > 0)
{
res += (n % 10)*pow(10, i--);
n /= 10;
}
return res;
}
int main()
{
long int n;
cout << "Please, enter a digit: ";
cin >> n;
cout << "Resulting number is ";
cout << ReverseDigit(n);
}
Comments
Leave a comment