0

What does this code do? Please explain me the statement inside of the while block?

import java.util.*; public class reversal { public static void main(String[] args) { Scanner in= new Scanner(System.in); System.out.println("Enter any number"); long n=in.nextLong(); System.out.println("You have entered "+n); long s,m=0; while(n>0) { s=n%10; m+=s; n/=10; m*=10; } m/=10; System.out.println("The number after reversal is "+m); } } }

18th Jun 2017, 5:21 PM
I Am RG
I Am RG - avatar
1 Resposta
+ 3
let's say the number you entered is 369 is n=369 greater than 0? yes, it is. s=369%10=9 (take a look at the modulus operator) m=9 n=36 m=90 is n=36 greater than 0? yes, it is. s=36%10=6 m=96 n=3 m=960 is 3 greater than 0? yes. s=3 m=963 n=0 m=9630 is 0 greater than 0? no. leave the loop. m=963 output m, which is the number you entered, but reversed.
18th Jun 2017, 5:28 PM
Mario L.
Mario L. - avatar