First traverse all the numbers from 3 to 200 using for loop. and take a variable to store the sum and add the numbers while traversing. Following is the working java code to calculate the sum of odd numbers from 3 to 200.
SumOdd.java:
----------------------------
class SumOdd{
public static void main(String s1[]){
int sum=0;//variable to store the sum
//Just traverse all the odd numbers from 3 to 200 using for loop
for(int i=3;i<=200;i++){
if(i%2 != 0){
//checks no. is not even
sum=sum+i;
}
}
System.out.print("The sum of all odd numbers from 2 to 200 = "+sum);
}
}
|