# Java Program to find Sum of Even and Odd Numbers

Write a Java Program to find Sum of Even and Odd Numbers using For Loop, and While Loop with example.

## Java Program to find Sum of Even and Odd Numbers using For Loop

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

```// Java Program to find Sum of Even and Odd Numbers using for loop
import java.util.Scanner;

public class SumofEvenOdd1 {
private static Scanner sc;
public static void main(String[] args)
{
int number, i, evenSum = 0, oddSum = 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;
}
else
{
oddSum = oddSum + i;
}
}
System.out.println("\n The Sum of Even Numbers upto " + number + "  =  " + evenSum);
System.out.println("\n The Sum of Odd Numbers upto " + number + "  =  " + oddSum);
}
}```

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

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

For Loop First Iteration: for(i = 1; i <= 5; i++)
if(i % 2 == 0) => if(1 % 2 == 0) – Condition is False. So, it enters into Else block

oddSum = oddSum + i
oddSum = 0 + 1 = 1

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) – Condition is False.

oddSum = oddSum + i
oddSum = 1 + 3 = 4

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

evenSum = evenSum + i
evenSum = 2 + 4 = 6

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

oddSum = oddSum + i
oddSum = 4 + 5 = 9

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

## Java Program to find Sum of Even and Odd Numbers using While Loop

This Java program to calculate sum of even and odd numbers is the same as the above, but we are using the While Loop.

```// Java Program to find Sum of Even and Odd Numbers using While loop
import java.util.Scanner;

public class SumofEvenOdd2 {
private static Scanner sc;
public static void main(String[] args)
{
int number, i, evenSum = 0, oddSum = 0;
sc = new Scanner(System.in);

System.out.print(" Please Enter any Number : ");
number = sc.nextInt();

i = 1;

while(i <= number)
{
if(i % 2 == 0)
{
evenSum = evenSum + i;
}
else
{
oddSum = oddSum + i;
}
i++;
}
System.out.println("\n The Sum of Even Numbers upto " + number + "  =  " + evenSum);
System.out.println("\n The Sum of Odd Numbers upto " + number + "  =  " + oddSum);
}
}```
`````` Please Enter any Number : 30

The Sum of Even Numbers upto 30  =  240

The Sum of Odd Numbers upto 30  =  225``````

## Java Program to find Sum of Even and Odd Numbers within a Range

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

```import java.util.Scanner;

public class SumofEvenOdd3 {
private static Scanner sc;
public static void main(String[] args)
{
int minimum, maximum, i, evenSum = 0, oddSum = 0;
sc = new Scanner(System.in);

System.out.print(" Please Enter the Minimum Limit value : ");
minimum = sc.nextInt();

System.out.print(" Please Enter the Maximum Limit value : ");
maximum = sc.nextInt();

for(i = minimum; i <= maximum; i++)
{
if(i % 2 == 0)
{
evenSum = evenSum + i;
}
else
{
oddSum = oddSum + i;
}
}
System.out.println("\n The Sum of Even Numbers from " + minimum + " to " + maximum + "  =  " + evenSum);
System.out.println("\n The Sum of Odd Numbers from " + minimum + " to " + maximum + "  =  " + oddSum);
}
}```
`````` Please Enter the Minimum Limit value : 15
Please Enter the Maximum Limit value : 100

The Sum of Even Numbers from 15 to 100  =  2494

The Sum of Odd Numbers from 15 to 100  =  2451``````