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: Sum of even numbers

Write a java program to calculate sum of all even numbers between 1 to n.

Sample input

25

Sample output

156

corejava x 352
programming x 168
Posted On : 2016-09-17 11:35:02.0
profile Boopalan - anyforum.in Boopalan
3730
up-rate
3
down-rate

Answers


import java.util.Scanner;

public class SumEven {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Integer number=input.nextInt();
int sum=0;
for(int i=1;i<=number;i++){
if(i%2==0){
sum=sum+i;
}
}
System.out.println(sum);
}
}

Posted On : 2016-09-17 21:46:57
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221600
Reply This Thread
up-rate
4
down-rate



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