In Java, to find the sum of all even numbers within a given range, we can use loop concepts such as while loops or for loops. In this article, we focus on learning through examples, detailed logic, and program explanations for better understanding.
For example, if the input range is 2 to 5, then the sum of all the even numbers within this range should be 6 (2+4=6).
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We need to write a java program to find sum of all even numbers in a given range. The program should take two input values as first number and last number as a range and then it should print sum of even numbers in that range as output. This action should be done in our program. Input range is inclusive in result. Let’s see an example,
Example
For the input range 1 and 5, Output should be 6
Explanation:
Input first number: 1
Input last number: 5
Even numbers between 1 to 5: 2 4
Sum: 2 + 4 = 6
Hence, for the input range (1,5), the sum of even numbers is 6.
Logic to find sum of all even numbers in a given range
To find the sum of even numbers within a given range, we first iterate over each number in the range. For each number, we check if it is divisible by 2 using the modulo operator (%). Specifically, we use the expression n%2. If the result is 0, the number n is even; otherwise, it is odd. We skip the odd numbers and only consider the even ones. Finally, we calculate and print the sum of all the even numbers.
Java Program to find sum of all even numbers in a given range
Program 1: Using while loop
In the below program we are using while loop to find sum of all even numbers in a given range.
import java.util.Scanner;
public class SumOfEvenNum {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter the first number");
int firstNumber = s.nextInt();
System.out.println("enter the last number");
int lastNumber = s.nextInt();
int value=firstNumber;
int sum=0;
while(value<=lastNumber) {
if(value%2==0)
sum += value;
value++;
}
System.out.println("sum: "+sum);
s.close();
}
}
Output:
Enter the first number
1
Enter the last number
6
sum: 12
Java program explanation to find sum of all the even numbers in a given range
- The java program ask the user to input two integers:
firstNumber
andlastNumber
. - These represent the starting and ending numbers of the range to be considered for calculating the sum of even numbers.
- It initializes variables
value
with the value offirstNumber
andsum
to 0 - It uses a
while
loop to iterate through each number fromfirstNumber
tolastNumber
, inclusive. - Within the loop, if the current number (
value
) is even (determined byvalue % 2 == 0
), it adds the current number to thesum
. - After each iteration, the value of
value
is incremented by 1 (value++
). - After the loop, it prints the sum of even numbers within the specified range.
- Finally, it closes the
Scanner
objects
to release system resources.
Program 2: Using for loop
In the below program we are using while loop to find all even numbers in a given range
import java.util.Scanner;
public class SumEvenNumbers {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number");
int number=s.nextInt();
System.out.println("Enter the last number");
int lastNumber=s.nextInt();
int sum=0;
for(int i =number; i<=lastNumber;i++) {
if(i%2==0)
sum +=i;
}
System.out.println("sum: "+sum);
s.close();
}
}
Output:
Enter the first number
3
Enter the last number
6
sum: 10
Java program explanation to print sum of all the even numbers in a given range
- The java program ask the user to input two integers:
number
andlastNumber
. - These represent the starting and ending numbers of the range to be considered for calculating the sum of even numbers.
- It initializes a variable
sum
to 0 to store the sum of even numbers. - It uses a
for
loop to iterate through each number fromnumber
tolastNumber
, inclusive. - Within the loop, if the current number (
i
) is even (determined byi % 2 == 0
), it adds the current number to thesum
. - After the loop, it prints the sum of even numbers within the specified range.
- Finally, it closes the
Scanner
objects
to release system resources.
Conclusion
In this tutorial we have learnt writing the Java program to find the sum of even numbers in a given range using while loop and for loop. Hope this tutorial helped you in understanding it.
You can also check our another programming tutorials
- Java Program to check whether a given number is Prime or not.
- Java Program to print all Prime numbers between in a given range.
- Java Program to find sum of all prime numbers in a given range.
- Java Program to check a given number is Perfect or not.
- Java Program to check all Perfect numbers in a given range.
Happy Coding !!