Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
Algorithm
To solve this problem, we may follow the following algorithm:
Start
Start a loop for i = 1 to 100
Check if i is divisible by 2
If true, print the number
If false, increment the number
End
The flow chart describing the algorithm’s control flow is as follows:
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 + " "); } } } }
You can also try this code with Online Java Compiler
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 + " "); } } }
You can also try this code with Online Java Compiler
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++; } } }
You can also try this code with Online Java Compiler
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++; } } }
You can also try this code with Online Java Compiler
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);.
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.