Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
3.
Algorithm
4.
Approach 1: Using For loop
4.1.
Program 1:
5.
Java
5.1.
Output:
5.2.
Program 2:
6.
Java
6.1.
Output
7.
Approach 2: Using While loop
7.1.
Program 1:
8.
Java
8.1.
Output:
8.2.
Program 2:
9.
Java
9.1.
Output:
10.
Approach 3: Using Recursion
10.1.
Program:
11.
Java
11.1.
Output:
12.
Frequently Asked Questions
12.1.
What is looping?
12.2.
What are the different types of loops?
12.3.
What is recursion?
12.4.
What is even number program in Java?
12.5.
How to print 1 to 20 even numbers in Java?
12.6.
How to print first 100 even numbers in Java?
13.
Conclusion
Last Updated: Mar 27, 2024
Easy

Java Program to Display Even Numbers from 1 to 100

Author Pankhuri Goel
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Did you know that 2 is the only even prime number? When a number is divisible by 2 and can be divided into two equal pairs, it is said to be an even number. In other words, all the numbers having 0,2,4,6, or 8 at their unit place are even numbers. 

Java Program to Display Even Numbers from 1 to 100

This article will use this property to display all even numbers between 1 and 100. Let’s see how!

Problem Statement

We are given a range from 1 to 100. Our aim is to find the numbers which are even in this range. 

Now, we can follow various approaches to solve this problem but first, let us see the basic algorithm to find the even numbers in this range.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Algorithm

To solve this problem, we may follow the following algorithm:

  1. Start
  2. Start a loop for i = 1 to 100
    1. Check if i is divisible by 2
      1. If true, print the number
      2. If false, increment the number
  3. End 

 

The flow chart describing the algorithm’s control flow is as follows: 
 

algorithm’s control flow

Approach 1: Using For loop

In this approach, we are using for loop. The loop is initialised for i = 1, and the loop works till the value of i becomes 100.

Now, for each iteration, we check if i is divisible by two and print it if the remainder is 0. After each iteration, the value of i is increased by 1.

We are following two different methods to solve the problem using for loop. The code and output of this approach are following.

Program 1:

  • Java

Java

class DisplayEvenNumbers

   public static void main(String args[])  
   { 
       int n=100; 
       System.out.print("List of even numbers from 1 to 100: \n"); 
       for (int i=1; i<=n; i++)  
       {
           if ( i%2 == 0 )
           {
               System.out.print(i + " ");
           }
       }
   } 
}

Output:

output

Program 2:

  • Java

Java

class DisplayEvenNumbers

   public static void main(String args[])  
   { 
       int n=100; 
       System.out.print("List of even numbers from 1 to 100: \n"); 
       for (int i=2; i<=n; i= i+2)  
       {
           System.out.print(i + " ");
       }
   } 
}

Output

output

Also check out Addition of Two Numbers in Java here.

Approach 2: Using While loop

In this approach, we are using a while loop. The value of i is initialised to 1. The condition of the loop is till the value of i becomes 100.

Now, for each iteration, we check if i is divisible by two and print it if the remainder is 0. After each iteration, the value of i is increased by 1.

This is almost similar to the approach we followed for For loop.

We are following two different methods to solve the problem using while loop. The code and output of this approach are following. 

Program 1:

  • Java

Java

class DisplayEvenNumbers

   public static void main(String args[])  
   { 
       int n=100, i=1; 
       System.out.print("List of even numbers from 1 to 100: \n"); 
       while (i<=n)  
       {
           if ( i%2 == 0 )
           {
               System.out.print(i + " ");
           }
           i++;
       }
   } 
}

Output:

output

Program 2:

  • Java

Java

class DisplayEvenNumbers

   public static void main(String args[])  
   { 
       int n=100, i=1; 
       System.out.print("List of even numbers from 1 to 100: \n"); 
       while (i<=n)  
       {
           if ( i%2 == 0 )
           {
               System.out.print(i + " ");
           }
           i++;
       }
   } 
}

Output:

output

Approach 3: Using Recursion

In this approach, we use the nested-if statement as well as Recursion

  • In this approach, we call the method ‘displayEvenNumbers’ from the main method.
  • Inside the displayEvenNumbers method, the recursive calls are made once we have reached our first even number. 
  • We print the following even number in each iteration until the condition (n>e) is false.
  • Nothing is returned once we reach the number outside our range, and the recursive function stops.

The code, input and output of this approach are following.

Program:

  • Java

Java

class DisplayEvenNumbers

   public static void main(String args[])  
   { 
       System.out.println("List of even numbers from 1 to 100: ");
       displayEvenNumbers(1, 100);  
   }
   private static void displayEvenNumbers(int n, int e)  
   {  
       if(n>e)  
           return;  
       if(n%2 == 0)  
       {  
           System.out.print(n + " ");  
           displayEvenNumbers(n + 2, e);  
       }  
       else  
       {  
           displayEvenNumbers(n + 1, e);  
       }  
   }  
}

Output:

output

You can easily run this code yourself with java online compiler.

Must Read: Java System Out Println

Must Read Conditional Statements in Java

Must Read What are Loops in Java.

Frequently Asked Questions

What is looping?

In programming languages, looping is a feature that allows a collection of instructions/functions to be executed again until some condition is true. The loops can be implemented in three different ways in Java. While all methods provide comparable basic functionality, they differ in syntax and the time it takes to check for conditions.
 

What are the different types of loops?

There are three basic types of loops in Java.
For loop: This loop iterates a part of the program several times. It is generally used when the number of iterations is fixed. 
Syntax: for ( initialization condition ; testing condition ; increment/decrement )
{
<block of statements>
}

While loop: This loop iterates a part of the program several times. It is generally used when the number of iterations is not fixed. 
Syntax: while ( boolean condition )
{
<block of statements>
}

Do-while loop: This loop iterates a part of the program several times. It is generally used when the number of iterations is not fixed, but the loop must run once.
Syntax: do
{
<block of statements>
} while ( condition ) ;

What is recursion?

Recursion is the process of a function calling itself directly or indirectly, and the associated function is known as a recursive function. Specific problems can be solved quickly using the recursive algorithm.

This strategy allows you to break down complex difficulties into smaller, easier-to-solve problems. Recursion might be a tricky concept to grasp. Experimenting with it is the most efficient method to discover how it works.

What is even number program in Java?

A Java program checks if a given number is even using the modulo operator (%). For example: if (num % 2 == 0) { /* even */ }.

How to print 1 to 20 even numbers in Java?

Use a loop to iterate from 1 to 20, checking and printing even numbers: for (int i = 1; i <= 20; i++) if (i % 2 == 0) System.out.println(i);.

How to print first 100 even numbers in Java?

You can print first 100 even numbers in Java using a loop for the range 1 to 100: for (int i = 1; i <= 100; i++) if (i % 2 == 0) System.out.println(i);.

Must read also -  Strong number in c

Conclusion

This article taught us how to display even numbers in the range of 1 to 100 in Java. We saw how to solve this problem using for loop, while loop and recursion.

We hope this blog has helped you enhance your knowledge. If you would like to learn more, check out our articles on Java Tokens and Java Verify. If you want to learn more about the basics of Java, follow this guided path Basics Of Java. Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more!

Previous article
Program to find Sum of Natural Numbers
Next article
Java Program to Find the Largest of Three Numbers
Live masterclass