You need to test if the number is odd and count how many odds you find.
int oddCount = 0;
int sum = 0;
for (int number = 1; number <= 100; ++number) { // for loop
if (number % 2 != 0) {
sum += number;
oddCount++;
}
}
System.out.println(sum / (double) oddCount)
Or, skip the even values with something like
int oddCount = 0;
int sum = 0;
for (int number = 1; number <= 100; number += 2) { // <-- skip the even values.
sum += number;
oddCount++;
}
System.out.println(sum / (double) oddCount)
0
solved Java (Sum Odd Numbers and Compute Average) [closed]