-1

I have a loop for palindrome checking and I want to ask why does it work when i do

b=b*10+a 

but when I do

b*=10+a

it doesn't work even though it's short for b=b*10+a.

int a=0,b=0;

while (number>0) {
    a=number%10;
    b=b*10+a;
    number/=10;
}

int a=0,b=0;

while (number>0) {
    a=number%10;
    b*=10+a;
    number/=10;
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 3
    `b=b*10+a ` and `b*=10+a` are not equivalent. `b*=10+a` is equivalent to `b = b * (10 + a)`. – Turing85 Mar 22 '22 at 18:07
  • 1
    Hint: when you do not understand what your code is doing: then PRINT intermediate steps, or learn how to use a debugger. It is that simple. For such simple code you do not need other people to tell you what is going on. And note: when asking for "not working code" then always follow [mcve] and give expected vs actual output. – GhostCat Mar 22 '22 at 18:09

2 Answers2

0

Because those are two completely different operations.

Let's say b=5 and a=2, then

b=b*10+a

is going to set b = 52, ( (5*10) + 2)

b*=10+a

is going to set b = 60, (5 * (10+2))

Ryan
  • 1,762
  • 6
  • 11
0
b=b*10+a 

computes b*10 + a and assignes the result as the new value of b.

b*=10+a

is equivalent to

b = b * (10 + a)

so it computes b * (10 + a) and assigns the result as the new value of b.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175