# Basic while loop issues

• October 8th, 2012, 01:47 AM
Litost
Basic while loop issues
In my first program I was asked to output the perfect squares less than the value n.
Code :

``` int i = 0; int x = 0; int n = in.nextInt(); while (i < n) { i = x*x; System.out.print(i + " "); x = (x + 1); }```

My issue is if I enter 90, it outputs 100. I know this is because after 9*9 it goes to 10*10 but how can I change it so it doesn't do this?

My second issue is a bit more complicated and I have no idea what to do. I have to output all positive numbers divisible by 10 less than n:

Code :

``` int i = 0; int x = 0; int n = in.nextInt(); while (i < n && i > 0) { i = x/n; if (i%10==0) { System.out.print(i + " "); } else { x = (n-1); } }```

my output is all zeros. Why?! How do I fix this?
• October 8th, 2012, 08:15 AM
Norm
Re: Basic while loop issues
Quote:

my output is all zeros. Why?
int x = 0;

Also: i = x/n;
if x < n because of integer math

Try debugging the code by adding some println statements to print out the values of all the variables as they are set and used.