The method evenSquares takes a single int parameter, n, (for example, 10000), an
ID: 3650834 • Letter: T
Question
The method evenSquares takes a single int parameter, n, (for example, 10000), and then prints all of the (positive) even perfect squares less than n, each on a separate line. Notice that evenSquares has a void return type, since all it does is print integers to the console. Be sure to use the println method to print each entry of your output.Example: if n = 40, your code should print
4
16
36
(Hint: your method should be built around a for loop with a test component that asks if the square of the control variable (say, k) is < n. Thus, the loop should terminate as soon as k*k equals or exceeds n.)
Something like this:
start at k =2
square k
if the squared result is >= 10000, stop
print the squared result
go to the next even k = k + 2
Explanation / Answer
please rate - thanks
import java.util.*;
public class main {
public static void main(String args[])
{Scanner in=new Scanner(System.in);
int n;
System.out.print("enter a number: ");
n=in.nextInt();
evenSquares(n);
}
public static void evenSquares(int n)
{int k=2;
System.out.println("Even Squares <"+n);
while(k<n)
{if(k*k<n)
System.out.println(k*k);
k+=2;
}
}
}