Java Code to Display Even Numbers From 1 to 100

In this section, we will write Java code to display even numbers from 1 to 100. To understand this program, you should have a basic understanding of Java’s for loop and if statements.

There are multiple approaches to displaying even numbers:

  1. Using a Java for Loop
  2. Using a nested if Statement
  3. Using a while Loop

Display Even Numbers Using Java for Loop

In the following example, we declare a variable called “number” and initialize it with the value 100, which represents the limit for printing even numbers. The program uses a for loop that iterates 100 times, incrementing the variable “i” with each iteration. Inside the loop, an if statement checks whether the current value of “i” is an even number. This is done by dividing “i” by 2 and checking if the remainder is zero. If the remainder is zero, the number is even, and the program prints it. After printing each even number, the value of “i” is increased by 1.

By utilizing this approach, we can identify and print all the even numbers within the given range.

DisplayEvenNumbersExample.java

public class DisplayEvenNumbersExample  
{  
public static void main(String args[])   
{  
int number=100;  
System.out.print("List of even numbers from 1 to "+number+": ");  
for (int i=1; i<=number; i++)   
{  
//logic to check if the number is even or not  
//if i%2 is equal to zero, the number is even  
if (i%2==0)   
{  
System.out.print(i + " ");  
}  
}  
}  
}

Output:

List of even numbers from 1 to 100: 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.

Display Even Numbers Using nested-if Statement

The following program differs slightly from the previous one as it includes a method that encapsulates the logic to check for even numbers. Within this method, a nested-if statement is used.

DisplayEvenNumbersExample1.java

public class DisplayEvennumbersExample1  
{  
public static void main(String[] args)   
{  
System.out.println("List of even numbers: ");       
//method calling  
displayEvenNumbers(1, 100);   
}   
//method that checks the number is even or not  
private static void displayEvenNumbers(int number, int end)   
{   
if(number>end)   
return;   
if(number%2==0)   
{   
//prints the even numbers  
System.out.print(number +" ");   
//calling the method and increments the number by 2 if the number is even  
displayEvenNumbers(number + 2, end);   
}   
else   
{   
//increments the number by 1 if the number is odd  
displayEvenNumbers(number + 1, end);   
}   
}   
}

Output:

List of even numbers: 
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

Display Even Numbers Using while Loop

In the following program, we have modified it by replacing the for loop with a while loop.

DisplayEvenNumbersExample2.java

import java.util.Scanner;  
public class DisplayEvenNumbersExample2  
{  
public static void main(String[] args)   
{  
int number, i;  
Scanner sc=new Scanner(System.in);  
System.out.print("Enter the limit: ");  
number = sc.nextInt();    
i=2;   
System.out.print("Lit of even numbers: ");  
//the while loop executes until the condition become false  
while(i<=number)  
{  
//prints the even number  
System.out.print(i +" ");   
//increments the variable i by 2  
i=i+2;  
}     
}  
}

Output:

Enter the limit: 100
Lit of even numbers: 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

In conclusion, the provided Java program effectively displays even numbers from 1 to 100 using a for loop and an if statement. The program checks each number in the given range and prints it if it is even. By utilizing modular logic within a method, the code becomes more organized and reusable. Furthermore, the program can be modified to use a while loop instead of a for loop while achieving the same result. Overall, this program showcases the basic usage of loops and conditional statements in Java to identify and display even numbers within a specified range.

Follow tutorials.freshersnow.com to learn more like Java Code to Display Even Numbers.