Hello, so in my computer science class we have been asked to write a good that reverses a number. I can get it to work for positive numbers, but not negative numbers, can you please help? Thanks.
here is my the function i am working on:
int reverseInt(int orig)
{
int rev = 0; // declare a variable for the reversed version
// and initialize it to 0
while (orig > 0) {
int curDig = orig % 10; // create a variable for the current digit,
// and initialize it to orig % 10
rev = (rev * 10 + curDig); // the new value for reversed is
// the old value * 10, plus the new digit
orig = (orig / 10); // drop the digit off of the original
// using orig = orig / 10
}
return rev; // return the reversed value
/*This is what my teacher wants us to do for the negative numbers:*/
// create a boolean variable for isNegative,
// initialized to false
// if the original is less than zero
// set isNegative to true
// and multiply the original by -1
// then have your while loop as per the previous stage
// afterwards, if isNegative is true,
// multiply the reversed value by -1
// afterwards have your normal return statement
/*And this is what i have attempted to do*/
bool isNegative = false;