How can an array be used to add all odd numbers in a certain range of numbers eg from 1-100 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How can an array be used to add all odd numbers in a certain range of numbers eg from 1-100

19th Feb 2017, 6:16 AM
Bukuru Henry
Bukuru Henry - avatar
2 Answers
+ 1
use a for loop from 1 to 100 and an If condition: if(variable%2!=0) sum+=variable.
19th Feb 2017, 6:23 AM
Sandip
+ 1
int sum=0; for(int i=1; i<=100; i+=2){ sum+=i; } System.out.print(sum);
19th Feb 2017, 6:25 AM
Vikas Gupta
Vikas Gupta - avatar