+ 5
Can you explain it for me?
What does this formula say? Explain it please.. digit = num % 10; rev = (rev * 10) + digit; num = num / 10;
7 Réponses
+ 9
num%10 is the remainder when you divide the number by 10. So it is actually the last digit of the number.
Then multiplying rev by 10, all of its digits are shifted to left and you add the digit that you calculated previously.
Then num/10 divides the number by 10, this shifts all digits to right and removes the last digit.
I imagine this is all inside a loop, reversing the order of the digits in the original number.
+ 5
Let's take a number, for example say, int num=145
and rev is initialized with value 0,int rev=0
digit=145%10 //% operator returns remainder
digit=5
rev=0*10+5=5
num=145/10=14
Assuming that the above code is in a while loop (while num>0), we start from first statement again,
digit=14 %10
digit=4
rev=5*10+4=54
num=14/10=1
Loop runs again as num is still greater than 0,
digit=1%10
digit=1
rev=54*10 + 1
rev=541
num=1/10=0
Since num is not greater than 0, loop stops and the number is reversed as well
145 is now 541.
+ 3
+ 2
We need to announce another r
0
It's a code to reverse a number like 123 to 321