Jump to content

guys what does this java code mean?

buklu

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

Link to comment
Share on other sites

Link to post
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

Link to comment
Share on other sites

Link to post
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?

Link to comment
Share on other sites

Link to post
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)

If you're interested in a product please download and read the manual first.

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

Link to comment
Share on other sites

Link to post
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.

Link to comment
Share on other sites

Link to post
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

Link to comment
Share on other sites

Link to post
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.

If you're interested in a product please download and read the manual first.

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

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×