Search In
• More options...
Find results that contain...
Find results in...

# guys what does this java code mean?

System.out.print("Integer in reverse order=");
while (n!=0) {
int digit = n%10;
System.out.print(digit);
n=n / 10;
}
System.out.println();

##### Share on other sites

while n is not equal to 0 do integer digit n remainder 10 then print that digit and n equal n divided by 10  then print blank line as spacer. looks a loop for getting all the tens places printed out

##### Share on other sites

hope that helps you

##### Share on other sites

14 minutes ago, Turretgaming said:

while n is not equal to 0 do integer digit n remainder 10﻿ then print that digit and n equal n divided by 10  then print blank line as spacer. looks a loop for getting all the tens places printed out

what is n remainder 10?

##### Share on other sites

say you have number 362

362 % 10 = 2 (int) 2 = 2

print 2

362 / 10 = 36.2

36.2 % 10 = 6.2  (int) 6.2 = 6

print 6

36.2 / 10 = 3.62

3.62 % 10 = 3.62  (int) 3.62 = 3

print 3

3.62 / 10 = 0.362

now presumably you would want it to stop here but n != 0 so it will keep adding 0 so the while loop should read while(n > 0)

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

##### Share on other sites

1 minute ago, keskparane said:

say you have number 362

362 % 10 = 2 (int) 2 = 2

print 2

362 / 10 = 36.2

36.2 % 10 = 6.2  (int) 6.2 = 6

print 6

36.2 / 10 = 3.62

3.62 % 10 = 3.62  (int) 3.62 = 3

print 3

3.62 / 10 = 0.362

now presumably you would want it to stop here but n != 0 so it will keep adding 0 so the while loop should read while(n > 0)

i agree. this way it wont loop for ever. what computer science class is this for btw i assume its for a class as in my freshman year we made this loop too.

thanks bro

##### Share on other sites

changing it to n > 0 makes no difference. 0.362 is greater then 0. So the loop will continue anyway. dividing by 10 forever. This loop will never end. i'd make it n > 1, so it will actually stop.

I have no signature

##### Share on other sites

10 hours ago, Helly said:

changing it to n > 0 makes no difference. 0.362 is greater then 0. So the loop will continue anyway. dividing by 10 forever. This loop will never end. i'd make it n > 1, so it will actually stop.

ah yes.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.