+ 1

I don't understand. Explain me this code from line 10 to 12?

public class practice { public static void main(String args[]) { int number = 1234; int reversednumber = 0; int temp = 0; while(number > 0) { temp = number % 10; reversednumber = reversednumber *10 + temp; number = number / 10; } System.out.println("Reversed number is :" + reversednumber); } }

4th Oct 2018, 11:22 AM
Nitin Gutte
Nitin Gutte - avatar
3 Réponses
+ 4
number(1234) > 0 true temp = 1234%10 = 4 reversednumber = 0*10 + 4 = 4 number = 1234/10 = 123 number(123) > 0 true temp = 123%10 = 3 reversednumber = 4*10 + 3 = 43 number = 123/10 = 12 number(12) > 0 true temp = 12%10 = 2 reversednumber = 43*10 + 2 = 432 number = 12/10 = 1 number(1) > 0 true temp = 1%10 = 1 reversednumber = 432*10 + 1 = 4321 number = 1/10 = 0 number(0) > 0 false. whilemloop terminates. Reversed number is 4321
4th Oct 2018, 11:40 AM
Andre Daniel
Andre Daniel - avatar
+ 3
Andre Daniel I think you meant to use the '%' symbol when dividing by 10 the first time in your examples.
4th Oct 2018, 12:44 PM
Zeke Williams
Zeke Williams - avatar
+ 2
Andre Daniel and Zeke Williams...Thank u
5th Oct 2018, 7:20 AM
Nitin Gutte
Nitin Gutte - avatar