In Java, to print all even numbers in a given range we can use loop concepts such as while loop or for loop. When the user enters any two values, it will print all the even numbers in the range that the user entered. Here inputs are inclusive.
For a given input range, suppose 1-10, we will check if the value is divided by 2, if divided by 2, then print even numbers. Here 2, 4, 6, 8 is even numbers and those numbers are printed as output. In this article we will learn the printing even number in given range with the help of examples, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We have to write a java program that will print 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 even numbers in that range as output. This action should be done in our program. Input values are inclusive here.
Let’s see an example
Example 1:
Input: enter the first number
1
enter the last number
100
Output: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
Here for the input range 1 to 100, Output is all the even numbers between in that range.
Example 2:
Input: enter the first number
3
enter the last number
8
Output: 4
6
8
Here for the input range 3 to 8, Output is all the even numbers between the range 3 to 8.
Logic to print all Even numbers in a given range
To print all 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 and print it as an output.
Java program to print all Even numbers in a given range
Program 1: Using while loop
In the below program we are using while loop to print all even numbers in a given range.
import java.util.Scanner;
public class EvenNumber {
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;
System.out.println("Even numbers are:");
while (value <= lastNumber) {
if (value % 2 == 0)
System.out.println(value);
value++;
}
s.close();
}
}
Output:
enter the first number
1
enter the last number
100
Even numbers are:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
Java program explanation to print all the even numbers between given range
- This Java program ask the user to input two integers:
firstNumber
andlastNumber
. - It reads these inputs using a
Scanner
objects
. - It then initializes a variable
value
with the value offirstNumber
. - Inside a
while
loop, it iterates fromfirstNumber
tolastNumber
. - Within the loop, it checks if the current value of
value
is even using the conditionvalue % 2 == 0
. - If the condition is true, it prints the current value of
value
, indicating it’s an even number. - After each iteration, the value of
value
is incremented by 1 (value++
). - Finally, it closes the
Scanner
objects
to release system resources.
Program 2: Using for loop
In the below program we are using for loop to print all even numbers in a given range.
import java.util.Scanner;
public class EvenNumbers {
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();
System.out.println("Even numbers are:");
for(int i= firstNumber; i<=lastNumber;i++) {
if(i%2==0)
System.out.println(i);
}
s.close();
}
}
Output:
Enter the first number
1
Enter the last number
8
Even numbers are:
2
4
6
8
Java program explanation to print 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 checked for even numbers.
- It uses a
for
loop to iterate through each number fromfirstNumber
tolastNumber
, inclusive. - Within the loop, it checks if the current number (
i
) is even using the conditioni % 2 == 0
. - If the condition is true, it prints the current number (
i
), indicating it’s an even number. - There is no input validation to ensure
firstNumber
is less than or equal tolastNumber
, assuming valid inputs from the user. - Finally, it closes the
Scanner
objects
to release system resources.
Conclusion
In the above program we have learnt writing the Java program to print all the even numbers in the given range. We have seen writing the program using for loop in java and while loop in Java. Hope this tutorial helped you in the understanding of the program.
You can also check our another awesome tutorials
- Java Program to print sum of all even numbers in a given range.
- Java Program to print sum of all odd numbers in a given range.
- Java Program to count the digits of a given number.
- Java Program to print the sum of digits of a given number.
Happy Coding !!