Use the Java Math square root method to calculate square roots from 1 to 1,000. The output the whole numbers only.Do not start by calculating squares and then printing out that number's square root. Calculate all square roots and determine if they are whole numbers.
I looked up several answers that seemed to dance around the solution but none of them so very concise. Here is what I have so far.
public class Assignment9
{
public static void main(String[] args)
{
double root = 1;
double sqrt = Math.sqrt(root);
do
{
if(sqrt % 1 == 0)
{
System.out.printf("%.0f\t%.0f%n", root, Math.sqrt(root));
root++;
}
}
while(root <= 1000);
}
}
The output is printing the square roots but keeps rounding up each number. I want to only print out one number per perfect square, (e.g. 1 1, 4 2, 9 3, etc). I understand the question, but every time I run the program I get this as my output:
1 1
2 1
3 2
4 2
5 2
6 2
7 3
8 3
9 3
10 3
...
1000 32
You aren't updating the value sqrt. It's always just 1, and thus sqrt % 1 is always 0.