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.)

# writing a method for an algorithm

Page 1 of 1## 3 Replies - 4718 Views - Last Post: 03 October 2012 - 10:51 AM

##
**Replies To:** writing a method for an algorithm

### #2

## Re: writing a method for an algorithm

Posted 03 October 2012 - 09:33 AM

Seems simple enough, have you given it a try? Anything specific you need help with?

### #3

## Re: writing a method for an algorithm

Posted 03 October 2012 - 09:59 AM

### #4

## Re: writing a method for an algorithm

Posted 03 October 2012 - 10:51 AM

System.out.println(Math.sqrt(k));

will print the square root of all the k specified

you want the exact square root and you want the input parameter to be even

I would rather go the other way:

- take all numbers

- raise them to of 2

- break out of the loop if you execeed the limits

- found if the result is even

will print the square root of all the k specified

you want the exact square root and you want the input parameter to be even

I would rather go the other way:

- take all numbers

- raise them to of 2

- break out of the loop if you execeed the limits

- found if the result is even

Page 1 of 1