AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Program to find out the sum of odd numbers

write a java program to find the sum of odd numbers from 3 to 200.

corejava x 340
programming x 157
Posted On : 2014-07-03 19:45:13.0
profile prince haxor - anyforum.in prince haxor
1930
up-rate
3
down-rate

Answers


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);
}
}

Posted On : 2014-07-03 22:07:08
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185319392
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in