I am trying to use a function to reverse numbers (ex: 123 to 321, -0065 to -56), I am having problems trying to convert numbers such as 7008 to 8007, anyone have any pointers?
#include <iostream>
#include <cmath>
usingnamespace std;
int reverseNumber (int userNumber);
int main()
{
int userNum;
int reversedNumber;
cout << "Enter a number, and I will reverse the number" << endl;
cin >> userNum;
cout << "The reversed number is: ";
reversedNumber = reverseNumber(userNum);
cout << reversedNumber;
}
int reverseNumber (int userNumber)
{
int tenToPower;
int reversedNumber;
double power = 0;
if (userNumber == 0)
{
return userNumber;
}//end of if statement
else
{
while (userNumber / static_cast<int> (pow (10, power)) >= 10 || userNumber / static_cast<int> (pow (10, power)) <= -10)
{
power++;
}// end of first while
while (userNumber >= 1 || userNumber <= -1)
{
tenToPower = static_cast<int> (pow (10, power));
if (userNumber % 10 != 0)
{
reversedNumber = (userNumber % 10) * tenToPower + reversedNumber;
userNumber = userNumber / 10;
power = power - 1;
}
}//end of second while
}//end of else statement
return reversedNumber;
}// end of function reverseNumber
#include <iostream>
#include <cmath>
usingnamespace std;
int reverseNumber (int userNumber);
int main()
{
int userNum;
cout << "Enter a number, and I will reverse the number" << endl;
cin >> userNum;
cout << "The reversed number is: "
<< reverseNumber(userNum) << endl;
}
int reverseNumber (int userNumber)
{
int tenToPower;
int reversedNumber;
double power = 0;
if (userNumber == 0)
{
return userNumber;
}//end of if statement
else
{
while (userNumber / static_cast<int> (pow (10, power)) >= 10 || userNumber / static_cast<int> (pow (10, power)) <= -10)
{
power++;
}// end of first while
while (userNumber >= 10 || userNumber <= -10)
{
tenToPower = static_cast<int> (pow (10, power));
if (userNumber % 10 != 0)
{
reversedNumber = (userNumber % 10) * tenToPower + reversedNumber;
power = power - 1;
}
}//end of second while
}//end of else statement
return reversedNumber;
}// end of function reverseNumber
Ok, I'm sorry about that. This is my code though, and when it asks for the input, and I enter a number, say 123, it does absolutely nothing. Also if I were to get rid of the endline; after the call, then I get an error saying that I should have ; after I try calling the function.