Java Program to Calculate Sum of Even Numbers

Write a Java Program to Calculate Sum of Even Numbers from 1 to N using For Loop, and While Loop with example. Any number that is divisible by 2 is an even number.

Java Program to Calculate Sum of Even Numbers Example 1

This Java program allows the user to enter the maximum limit value. Next, this Java program finds the sum of even numbers from 1 to maximum limit value using For Loop and If statement.

TIP : We already explained the logic to check the number is Even or Not in Java Odd or Even Program article in Java.

// Java Program to Calculate Sum of Even Numbers using for loop
import java.util.Scanner;

public class SumofEven1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i, evenSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		for(i = 1; i <= number; i++)
		{
			if(i % 2 == 0)
			{
				evenSum = evenSum + i; 
			}
		}
		System.out.println("\n The Sum of Even Numbers upto " + number + "  =  " + evenSum);
	}
}
Java Program to Calculate Sum of Even Numbers 1

First, we used the For loop to iterate from 1 to maximum value (Here, number = 5). Next, the If condition to check whether the remainder of the number divided by 2 is exactly equal to 0 or not.

User entered value for this Java Program to find Sum of Even Numbers : number  = 5

For Loop First Iteration: for(i = 1; i <= 5; i++)
if(i % 2 == 0) => if(1 % 2 == 0) – Condition is False

Second Iteration: for(i = 2; 2 <= 5; 2++)
if(2 % 2 == 0) – Condition is True.
evenSum = evenSum + i
evenSum = 0 + 2 = 2

Third Iteration: for(i = 3; 3 <= 5; 3++)
if(3 % 2 == 0) – False

Fourth Iteration: for(i = 4; 4 <= 5; 4++)
if(4 % 2 == 0) – True.
evenSum = evenSum + i
evenSum = 2 + 4 = 6

Fifth Iteration: for(i = 5; 5 <= 5; 5++)
if(5 % 2 == 0) – False

Sixth Iteration: for(i = 6; 6 <= 5; 6++)
Condition (6 <= 5) is False. So, the Java compiler exits from the For Loop

Java Program to Calculate Sum of Even Numbers Example 2

In this Java program, we used for loop without the If statement. If you observe the below Java code, we started i from 2 and incremented by 2 (not 1). It means, for the first iteration i = 2, and for the second iteration i = 4 (not 3) so on.

// Java Program to Calculate Sum of Even Numbers
import java.util.Scanner;

public class SumofEven2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i, evenSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		for(i = 2; i <= number; i = i + 2)
		{
			evenSum = evenSum + i; 
		}
		System.out.println("\n The Sum of Even Numbers upto " + number + "  =  " + evenSum);
	}
}

Java Sum of Even Numbers using for loop output

 Please Enter any Number : 25

 The Sum of Even Numbers upto 25  =  156

Java Program to Calculate Sum of Even Numbers Example 3

This Java program to find the sum of even numbers is the same as the second example, but we are using the While Loop.

// Java Program to find Sum of Even Numbers using While Loop
import java.util.Scanner;

public class SumofEven5 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i = 2, evenSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		while(i <= number)
		{
			evenSum = evenSum + i; 
			i = i + 2;
		}
		System.out.println("\n The Sum of Even Numbers upto " + number + "  =  " + evenSum);
	}
}
 Please Enter any Number : 35

 The Sum of Even Numbers upto 35  =  306

Java Program to Calculate Sum of Even Numbers using Method

This program is the same as the first example. But we separated the logic and placed it in a separate method.

// Java Program to find Sum of Even Numbers
import java.util.Scanner;

public class SumofEven3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, evenSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();
		
		evenSum = sumOfEven(number);
		
		System.out.println("\n The Sum of Even Numbers upto " + number + "  =  " + evenSum);
	}
	public static int sumOfEven(int num)
	{
		int i, sum = 0;
		for(i = 1; i <= num; i++)
		{
			if(i % 2 == 0)
			{
				sum = sum + i; 
			}
		}
		return sum;
	}
}
 Please Enter any Number : 45

 The Sum of Even Numbers upto 45  =  506

Java Program to find Sum of Even Numbers between a Given Range

This Java program allows the user to enter Minimum and maximum value. Next, the Java program calculates the sum of even numbers between Minimum value and maximum value.

// Java Program to Calculate Sum of Even Numbers between Maximum and Minimum
import java.util.Scanner;

public class SumofEven4 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int minimum, maximum, evenSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Minimum value : ");
		minimum = sc.nextInt();	
		
		System.out.print(" Please Enter the Maximum value : ");
		maximum = sc.nextInt();	
		
		evenSum = sumOfEven(minimum, maximum);	
		System.out.println("\n The Sum of Even Numbers from " + minimum + " to " + maximum + "  =  " + evenSum);
	}
	
	public static int sumOfEven(int minimum, int maximum)
	{
		int i, sum = 0;
		if(minimum % 2 != 0)
		{
			minimum++;
		}
		for(i = minimum; i <= maximum; i++)
		{
			if(i % 2 == 0)
			{
				sum = sum + i;  
			}
		}
		return sum;
	}
}
 Please Enter the Minimum value : 10
 Please Enter the Maximum value : 75

 The Sum of Even Numbers from 10 to 75  =  1386