0
Change this while loop to for loop
//Outputs triangular numbers class forExample2 { public static void main(String[] args) { int triangularNumber = 1; int y = 1; while(y <= 10){ System.out.println(triangularNumber); y++; triangularNumber = triangularNumber + y; } } }
4 Respostas
0
ChaoticDawg why triangularNumber + y + 1?
+ 1
for (int y = 1; y <= 10; y++) {
System.out.println(triangularNumber);
triangularNumber = triangularNumber + y + 1;
}
+ 1
Ian Karuchiu
From your code, you were incrementing y prior to incrementing triangularNumber. So, I added 1 to keep the math the same, since y is incrementing after triangularNumber is incremented in the for loop.
0
//It's so easy! 8D
class forExample2 {
public static void main(String[] args) {
for(int y = 1, tn = 1; y <= 10; tn += ++y)
System.out.println(tn);
}
}