Can you explain it for me? | SoloLearn: Learn to code for FREE!

+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;

1/23/2021 9:18:40 AM

Mubarak Hussain

7 Answers

New Answer

+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.

+4

Page2 Mubarak Hussain ... no/=10 //such as //123 //123 -> digit=3 // //reversed=0*10+3== //3 // //no/=10== //12 //_____ //12 //12 -> digit=2 // //reversed=3*10+2== //32 // //no/=10== //1 //_____ //1 //1 -> digit=1 // //reversed=32*10+1== //321 // //no/=10== //0 //_____ //the loop terminated because //n>0 is false // } } lower explanation: 1:do it in a time that number is greater than 0 2:get the last digit in number 3:put it digit to a number from right,and other digits if to be exist must go left 4:delete last digit of number for next digit(left of past digit)

+3

《 Nicko12 》 Elizabeth🌸 @

+3

In the name of kindly generous ALLAH Hello Mubarak Hussain reversed the digits . Such as 123456789 ->987654321 the modulu operator (remain of the division) of any number to 10 returns (be asnwer equal to) last digit on your number Such as 1234%10==4 2732638%10==8 10%10==0 1%10==1 and in normal dividing between two number these are not decimal the answer return an non-decimal (integer) even the real answer is decimal. Such as 12/2==6 1/2==0 -1/2==0 -3/2==-1 12/5==2 and for ten we have 12345/10==1234 1234/10==123 123/10==12 12/10==1 1/10==0 It can help us for to have a number without his last digit Now in your question algorithm: Of course it is in a loop Such as f(int no){ Int reversed=0; While(no>0){ //Get the no's last digit int digit=no%10 //Put digit on reversed //By put that digit //On end of reversed value reversed=reversed*10+digit //We multipaction to 10 //Because the past digit //That has gotten from //from no go to the left next2

+2

0

It's a code to reverse a number like 123 to 321