-6

I'm new to C++.
So I found this reverse code on the internet. My prof. told me to make a palindrome checker, so I look for the reverse first.
Here is what I made

void main() 
{
    int num;
    int new_num = 0;
    int dummy;

    cout << "Masukkan angka";
    cin >> num;

    dummy = num;
    cout << dummy << endl;

    while (num > 0)
    {
        new_num = new_num*10 + (num % 10);
        num = num/10;
    }
    cout << new_num << endl;

    if ( new_num == dummy)
    {
        cout << "true";
    }
    else 
        cout<<"false";

    getch();
}

The most confusing part is this

while(num > 0)
{
    new_num = new_num*10 + (num % 10);
    num = num/10;
}
cout << new_num << endl;

I found this on the internet and I don't know how it works. Can someone explain how this code can reverse the number I input? Like when I input 12345, the output would be 54321. I can't understand.

6
  • 2
    Try using a debugger or drawing with pencil and paper. Commented Sep 26, 2014 at 14:55
  • 1
    Look up for modulus operation , multiplication, division and addition, and some basics how the decimal numbers system works. Commented Sep 26, 2014 at 14:56
  • *10, %10 and /10 operations typically signify operations on decimal digits. Commented Sep 26, 2014 at 14:56
  • 1
    Do you understand the expression % 10? Commented Sep 26, 2014 at 14:56
  • 4
    Horrible formatting, and no idea how much you need explained: unclear/too broad Commented Sep 26, 2014 at 14:56

3 Answers 3

0

Its talking the last char of your input by using modulo.

input is: 12345

Cycle 1:

  1. new_num is 0, multiply by 10 give 0.
  2. add modulo or your input 12345 % 10 = 5, new_num is now 5.
  3. divide your input by 10 to remove last digit. input = 1234

Cycle 2,3,4 etc.:

  1. new_num is 5, multiply by 10 gives 50.
  2. add modulo or your input 1234 % 10 = 4, new_num is now 54.
  3. divide your input by 10 to remove last digit. input = 123
0

I have taken the liberty to modify your source, and provide it in a more readable format

int inputNumber = 123;
int reversedNumber = 0;

cout << "The input number is " << inputNumber << endl;

while (inputNumber > 0) {
    reversedNumber = reversedNumber*10 + inputNumber%10;
    inputNumber = inputNumber/10;
}
cout << "The reversed number is " << reversedNumber << endl;

First of all, always use meaningful variables.
It will be more helpful while reading your code.

Now on to the explanation,
The modulo operator gives the remainder (not quotient!) of a division, i.e. 5%2 = 1.

In the while loop,
1) We keep over-writing reversedNumber by changing (increasing) its decimal position (multiply by 10) and adding the remainder of the division of inputNumber and 10
2) We over-write reversedNumber by changing (decreasing) its decimal position (divide by 10).

I encourage you to actually trace out the program on paper, or printing the variable's values (debugging?) to see how this operation works.

-1

First take a look at what num % 10 does:

#include <iostream>

int main() {
  for (int i = 0; i < 100; ++i) {
    std::cout << i << " % 10 = " << i % 10 << '\n';
  }
}

http://coliru.stacked-crooked.com/a/8d26b893682b0001

Do you see the pattern? This expression basically just gets the least significant decimal digit from a number.

You should know the pattern in new_num*10 without even looking, since we learned this in elementary school math: It shifts the digits to the left one place.

Then if you think about what addition will do in this case, new_num*10 + num % 10, you should see that adding a number in the range [0..9] to an integer where the lowest digit is zero, will just replace that zero with the new digit.

num/10 shifts digits to the right, and since it's using integral division the fractional parts are discarded.

So all together we have:

int remove_last_digit(int value) { return value / 10; }

int get_last_digit(int value) { return value % 10; }

int push_digit(int value, int digit) {
  return value * 10 + digit;
}

bool has_more_digits(int value) { return value != 0; }

int reverse_int(int value) {
  int reversed_value = 0;
  while ( has_more_digits(value)) {
    reversed_value = push_digit(reversed_value, get_last_digit(value));
    value = remove_last_digit(value);
  }
  return reversed_value;
}

Think of a number as digits piled on top of each other. If you take the top digit and put it on top of a second pile, and keep doing that until the first pile is empty, then the digits in the second pile are now stacked in the reverse order.

Not the answer you're looking for? Browse other questions tagged or ask your own question.