KadirMYilmaz KadirMYilmaz - 1 year ago 63
Java Question

for loop that produces series of different numbers

I have found a few similar topics to my issue, but couldn't figure out the task still, so I thought it'd be best to create my own topic.

I need to write a for loop that produces the following output:

289 256 225 196 169 144 121 100 81

For added challenge, try to modify your code so that it does not need to use the * multiplication operator.

This is my code below, I'm stuck here so please help.

public class Exercises2{
public static void main(String[] args){
int start = 19;
int increment = 2;
for(int count = 81; count <= 289; count++){

System.out.println(count + start);
start = increment + start;


Answer Source

Below is what you need. Notice the count+=start increment within the for loop and the start+=increment adding from the base of 17 so you increase count by 19 the first time, 21 the second, etc.

Remember a for loop doesn't require a count++ it can be any valid command in the last portion or can be left out completely

int start = 17;
int increment = 2;
for(int count = 81; count <= 289; count+=start){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download