In the previous article, we have discussed about Java Program to Find Sum of all Odd Numbers between 0 to N
In this article we will see how to find sum of all even numbers between 0 to N by using Java programming language.
Java Program to Find Sum of all Even Numbers between 0 to N
A number is said to be even number if is divisible by 2.
Means, if the number is even then number % 2 will result 0.
For example:
Number = 16, As 16 % 2 = 0, So it is an even number. Number = 33, As 33 % 2 = 1, So, it is not an even number.
Let’s see different ways to find sum of all even numbers between 0 to N.
Method-1: Java Program to Find Sum of all Even Numbers between 0 to N By Using Static Input Value
Approach:
- Declare integer variable
Nand initialize the value of it. It is the number up to which we will check even numbers add will find sum. - Declare integer variable
evenSumand initialize value to 0, it will hold the result i.e. sum of all even numbers. - Using for loop, iterate from 0 to N.
- For every number
i, check whether it is an even number or not.if(i%2 == 0)theniis an even number else odd. - Add all even numbers in a
evenSumvariable.
Program:
import java.util.*;
public class Main
{
public static void main(String args[])
{
//Scanner class object created
Scanner in = new Scanner(System.in);
//integer variable 'N' taken to hold an integer number
//upto which we will check even numbers and will find sum
int N;
//integer variable 'evenSum' declared and initialized to 0
int evenSum = 0;
//value initialized
N = 30;
//for loop to iterate from 0 to N
for(int i = 0; i <= N; i++)
{
//if number is even then add it to 'evenSum'
if((i%2) == 0)
{
evenSum += i;
}
}
//printing result
System.out.print("Sum of all even numbers between 0 to "+ N + " = " + evenSum);
}
}
Output: Sum of all even numbers between 0 to 30 = 240
Method-2: Java Program to Find Sum of all Even Numbers between 0 to N By Using User Input Value
Approach:
- Declare integer variable
Nand take the value of it as user input using Scanner class. It is the number up to which we will check even numbers add will find sum. - Declare integer variable
evenSumand initialize value to 0, it will hold the result i.e. sum of all even numbers. - Using for loop, iterate from 0 to N.
- For every number
i, check whether it is an even number or not.if(i%2 == 0)theniis an even number else odd. - Add all even numbers in a
evenSumvariable.
Program:
import java.util.*;
public class Main
{
public static void main(String args[])
{
//Scanner class object created
Scanner in = new Scanner(System.in);
//integer variable 'N' taken to hold an integer number
//upto which we will check even numbers and will find sum
int N;
//integer variable 'evenSum' declared and initialized to 0
int evenSum = 0;
//taking the number value input from user
System.out.print("Enter a number: ");
N = in.nextInt();
//for loop to iterate from 0 to N
for(int i = 0; i <= N; i++)
{
//if number is even then add it to 'evenSum'
if((i%2) == 0)
{
evenSum += i;
}
}
//printing result
System.out.print("Sum of all even numbers between 0 to "+ N + " = " + evenSum);
}
}
Output: Enter a number: 5 Sum of all even numbers between 0 to 5 = 6
Method-3: Java Program to Find Sum of all Even Numbers between 0 to N By Using User Defined Method
Approach:
- Declare integer variable
Nand take the value of it as user input using Scanner class. It is the number up to which we will check even numbers add will find sum. - Declare integer variable
evenSumand initialize value to 0, it will hold the result i.e. sum of all even numbers. - Then call a user defined method findSum and pass
NandevenSumas parameter. - Inside the method, using for loop, iterate from 0 to N.
- For every number
i, check whether it is an even number or not.if(i%2 == 0)theniis an even number else odd. - Add all even numbers in a
evenSumvariable.
Program:
import java.util.*;
public class Main
{
public static void main(String args[])
{
//Scanner class object created
Scanner in = new Scanner(System.in);
//integer variable 'N' taken to hold an integer number
//upto which we will check even numbers and will find sum
int N;
//integer variable 'evenSum' declared and initialized to 0
int evenSum = 0;
//taking the number value input from user
System.out.print("Enter a number: ");
N = in.nextInt();
//calling user defined method findSum()
findSum(N, evenSum);
}
public static void findSum(int N, int evenSum)
{
//for loop to iterate from 0 to N
for(int i = 0; i <= N; i++)
{
//if number is even then add it to 'evenSum'
if((i%2) == 0)
{
evenSum += i;
}
}
//printing result
System.out.print("Sum of all even numbers between 0 to "+ N + " = " + evenSum);
}
}
Output: Enter a number: 10 Sum of all even numbers between 0 to 10 = 30
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Related Java Programs:
- Java Program to Find the Difference Between the Sum of Positive Numbers and Negative Numbers
- Java Program to Find the Product Between the Sum of Positive Numbers and Negative Numbers
- Java Program to Calculate Remaining Loan Amount to Pay Back
- Java Program to Convert Acer to Hectare and Hectare to Acer