Java Program to calculate sum of Even numbers till N

Get input n and calculate the sum of even numbers till n

Sample Input 1:


Sample Output 1:

The Sum of Even Number till N:6(2+4)

Program or Solution

import java.util.*;
class SumOfNEven
    public static void main(String args[])
       int i,j,N,sum=0;
       System.out.println("Enter The Value Of N");
       Scanner sc=new Scanner(;

       System.out.println("The Sum Of N Even Numbers: "+sum);

Program Explanation

1. Get input N (To which even numbers to be added)

2. Instruction(s) inside the for block{} are executed repeatedly till the second expression (j<=N) is true.


3. Here i is initialized to 2 and incremented by 2 for each iteration, instructions inside the for block are executed      in every iteration. iteration stops when i becomes greater than n.

    so value of i (2,4,6,...n)will be added to sum.

    sum = sum + i 

    sum = 0 + 2

    sum = 2 + 4

    sum = 6 + 6


after adding all  even numbers till N to sum, sum will be printed using system.out.println.