I have an array of 100 numbers and I only gave the array even values. How can I print out how many elements of the array I have to add to obtain a sum < than 1768 using a WHILE LOOP? The following is what I have so far and I am stuck… thanks in advance for the help
void setup() {
int[] x = new int[100];
int i=0;
int sum=0;
for(i=0; i<100; i++) {
if (i%2==0) {
x[i]=i;
sum+=x[i];
}
}
}
You start with the even index of 0. Then just skip odd numbers by using
i += 2.If the number of elements is limited with 100, add
i < 200to thewhilecondition:The array of 100 even numbers will contain numbers from 0 to 200.
The variable
counterwill contain the number of additions performed. Its value will be equal toi / 2, so you can remove that additional variable.