void setup() { int i = 0; int sum = 0; int counter = 0; while (sum < 1768) { sum += i; i += 2; counter++; } System.out.println(counter); }
You start with an even index 0. Then just skip the odd numbers using i += 2 .
If the number of elements is limited to 100, add i < 200 to the while condition:
while (sum < 1768 && i < 200)
An array of 100 even numbers will contain numbers from 0 to 200.
The counter variable will contain the number of additions completed. Its value will be equal to i / 2 , so you can remove this additional variable.
source share