Reversing a number in C++

  • by theman on March 22, 2011 @ 01:55 AM
    Hi, I was asked to to a program that reverses the number that the user inputs. It should reverse the integer no matter it's length. It's for a homework and I've looked at other places but it is just for numbers with a fixed length. It is asking me to write a function that takes care of that. If possible can you write the whole program including the prototypes and everything. Thanks
    Learner 14 years ago

  • by ThePhantom on March 22, 2011 @ 05:36 AM
    Hi. Thanks for being a member of Digital Phantom Here is the code for reversing the number. With the comments, it's self explanatory but I'm going to explain you the logic anyways.
    1. #include <iostream>
    2.  
    3. using namespace std;
    4.  
    5. //function prototype
    6. int reverse(int);
    7.  
    8. int main(){
    9.  
    10. // keep program running for multiple executions
    11. while(cin){
    12.  
    13. int number;
    14.  
    15. cout << "Please enter a number to reverse: " << endl;
    16.  
    17. cin >> number;
    18.  
    19. cout << "\nReverse:\t" << reverse(number) << "\n" << endl;
    20. }
    21.  
    22. return 0;
    23. }
    24. //function declaration
    25. int reverse(int number){
    26.  
    27. //declare the digit variable and the reverse number variable.
    28. //initialize reverse number to 0
    29. int digit, reverseNumber = 0;
    30.  
    31. //get the absolute value of the number
    32. number = abs(number);
    33.  
    34. //if number is 0 then just output 0
    35. if(number == 0){
    36. return 0;
    37. }
    38. else {
    39. //while the number is greater than 0 then keep looping
    40. while(number > 0){
    41.  
    42. //get the last digit from right to left
    43. digit = number % 10;
    44. //trim the last digit
    45. number /= 10;
    46.  
    47. //create the rever number with each digit
    48. reverseNumber = reverseNumber * 10 + digit;
    49.  
    50. }
    51.  
    52. // return the number
    53. return reverseNumber;
    54.  
    55. }
    56.  
    57. }
    If you were going to do this by hand you will start writing the digits from right to left; so in programming with do this by taking the remainder of the number when we divide it by 10. For example: 10 % 10 = 0 so that will be the last digit. Now we divide that by ten and the result is 1. Once again we get the remainder 1 % 10 = 1, and thats the other digit. Now we put the the digits together to form the reverse number.
    Last Updated by: ThePhantom @ Mar 22, 2011 @ 03:45 PM
    Programmer 14 years ago

  • by theman on March 22, 2011 @ 08:37 PM
    Thanks @ThePhantom for the code. I'm sorry for this question as it might sound kinda dumb, but I still don't get this part of the code can you explain it please; cause i need to explain the process of developing the code in my assignment.
    1. while(number > 0){
    2. //get the last digit from right to left
    3. digit = number % 10;
    4. //trim the last digit
    5. number /= 10;
    6. //create the rever number with each digit
    7. reverseNumber = reverseNumber * 10 + digit;
    8. }
    Learner 14 years ago

  • by ThePhantom on March 23, 2011 @ 12:41 AM
    Hi @theman. Basically the purpose of this piece of code is to take a number and break it into digits from right to left.
    1. while(number > 0){
    2. //get the last digit from right to left
    3. digit = number % 10;
    4.  
    5. //trim the last digit
    6. number /= 10;
    7.  
    8. //create the rever number with each digit
    9. reverseNumber = reverseNumber * 10 + digit;
    10. }
    Let's take an example. number is an int variable so let's say number = 1234. Remember that digit is an int variable and that reverseNumber is initialized to 0. Now we enter the while loop. The following shows the iterations of the loop: 1. digit = 1234 % 10; therefore digit = 4. number /= 10; therefore number = 123 since number is an int. reverseNumber = 0 * 10 + 4; so reverseNumber = 4. 2. digit = 123 % 10; therefore digit = 3. number /= 10; therefore number = 12 since number is an int. reverseNumber = 4 * 10 + 3; so reverseNumber = 43. 3. digit = 12 % 10; therefore digit = 2. number /= 10; therefore number = 1 since number is an int. reverseNumber = 43 * 10 + 2; so reverseNumber = 432. 4. digit = 1 % 10; therefore digit = 1. number /= 10; therefore number = 0 since number is an int. reverseNumber = 432 * 10 + 1; so reverseNumber = 4321. Once number = 0; our loop condition becomes false and the program exits the loop. @theman, I hope this helps you with your assignment. Anyways we are here to help.
    Programmer 14 years ago

  • by theman on March 25, 2011 @ 12:48 AM
    Thanks @ThePhantom. I submitted the assignment and got 100%. You are awesome. Once again, thanks. I like the site, but I think it needs more members to allow more interaction. Thanks.
    Learner 14 years ago