I am trying to produce the following results:
120
200 202
280 284 288
360 366 372 378
440 448 456 464 472
520 530 540 550 560 570
600 612 624 636 648 660 672
680 694 708 722 736 750 764 778
760 776 792 808 824 840 856 872 888
As you can see, each row increases by 80 and difference between consecutive numbers in each row is i*2 (where i row number starting with 0)
I have written this code down which actually makes this result:
public class Triangle
{
public static void main(String [] args)
{
System.out.println(120);
System.out.print(i + " ");
}
System.out.println();
for (int i = 280 ; i <= 288; i = i + 4 ) {
System.out.print(i + " ");
}
System.out.println();
System.out.print(i + " ");
}
System.out.println();
for (int i = 440 ; i <= 472; i = i + 8 ) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 520 ; i <= 570; i = i + 10 ) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 600 ; i <= 672; i = i + 12 ) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 680 ; i <= 778; i = i + 14 ) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 760 ; i <= 888; i = i + 16 ) {
System.out.print(i + " ");
}
System.out.println();
}
}
However, this code is really long. You can see that I add 80 myself every time. Is there a way I could add another forever loop that does that instead? An adds the 2s as well?
Thanks
forloop. There is alsowhileanddo{}while. Also all of these loops can be infinite :)