Sum of Even Numbers in Java

By | February 28, 2016

Calculate Sum of Even Numbers in Java Programming

Here’s a Simple Program To Calculate Sum of Even Numbers in Java Programming Language. It is important to know how to use Modulus Operator in Java.

Even Numbers

An Even Number is the one which is Divisible by 2.The condition to check Odd Numbers is num%2==0

Odd Numbers

An Odd Number is the one which is not Divisible by 2. The condition to check Odd Numbers is num%2!=0

Also Read: Calculate Sum of Odd Numbers in Java

Program To Find Sum of Even Numbers in Java Programming

import java.io.*;
import java.util.*;

class SumOfEven
{
    public static void main(String[] arguments)
    {
        int limit, sum = 0, count;
        System.out.println("\nEnter The Limit");
        Scanner sc = new Scanner(System.in);
        limit = sc.nextInt();
        for(count = 0; count <= limit; count++)
        {       
            if(count%2 == 0)
                sum = sum + count;
            else
                continue;
        }
        System.out.println("\nSum of Even Numbers: " + sum);
    }
}

Also Read: Print Prime Numbers from 1 To N in Java

 

Output

How To Calculate Sum of Even Numbers in Java Programming Language

To know more about Odd and Even Numbers, Refer MathsIsFun. In case you get any Compilation Errors with this Java Program To Add Odd Numbers or you have any doubt about it, mention it in the Comment Section.

Let's Discuss