An integer (never a fraction) that can be divided exactly by 2. For example, 10 is an even number, i.e., 10 % 2 = 0.
Note: % is a Modulus (Division Remainder) operator, it finds the remainder after division of one number by another. Please check our Arithmetic Operators for more details.
In the following example, we will find all the Even Numbers between 10 and 25 using for loop.
Example
Java Compiler
public class myClass
{
public static void main(String[] args)
{
int start =10;
int end =25;
System.out.format("Even numbers between %d and %d:\n", start, end);
for(start=start; start<=end; start++)
{
if(start %2==0)
System.out.print(start +" ");
}
}
}
Output
Even numbers between 10 and 25:
10 12 14 16 18 20 22 24
Using While Loop
In the following example, we will find all the Even Numbers between 10 and 25 using while loop.
Example
Java Compiler
public class myClass
{
public static void main(String[] args)
{
int start =10;
int end =25;
System.out.format("Even numbers between %d and %d:\n", start, end);
while(start <= end)
{
if(start %2==0)
System.out.print(start +" ");
start++;
}
}
}
Output
Even numbers between 10 and 25:
10 12 14 16 18 20 22 24
Using do while Loop
In the following example, we will find all the Even Numbers between 10 and 25 using do while loop.
Example
Java Compiler
public class myClass
{
public static void main(String[] args)
{
int start =10;
int end =25;
System.out.format("Even numbers between %d and %d:\n", start, end);
do {
if(start %2==0)
System.out.print(start +" ");
start++;
}while(start <= end);
}
}
Output
Even numbers between 10 and 25:
10 12 14 16 18 20 22 24
Even Numbers between any Given Range
In the following example, we will find all the Even numbers between any given range.
Example
Java Compiler
import java.util.Scanner;
public class myClass
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
System.out.print("Enter a (int) starting number: ");
int start = reader.nextInt();
System.out.print("Enter an (int) ending number: ");
int end = reader.nextInt();
System.out.format("\nEven numbers between %d and %d:\n", start, end);
for(start=start; start<=end; start++)
{
if(start %2==0)
System.out.print(start +" ");
}
}
}
Output
Enter a (int) starting number: 5
Enter an (int) ending number: 15
Even numbers between 5 and 15:
6 8 10 12 14
Check Whether the Given Number is Even or Odd
In the following example, we will check whether the given number is an Even number or Odd number.
Example
Java Compiler
import java.util.Scanner;
public class myClass
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
System.out.print("Enter a (int) number: ");
int num = reader.nextInt();
if(num %2==0)
System.out.format("%d is an even number", num);
elseSystem.out.format("%d is an odd number", num);
}
}
Output
Enter a (int) number: 8
8 is an even number
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.
We are working to cover every Single Concept in Java.