Write a program to print all even numbers between 1 to 100


  • The Even_Numbers class is defined and the main method is declared.
  • Inside the main method, a Scanner object input is created to accept input from the user.
  • The program prompts the user to enter the limit up to which the even numbers should be printed.
  • The program reads the user's input using the nextInt() method of the Scanner object and stores it in an integer variable l.
  • The program enters a for loop that will execute from 1 to the value of l.
  • Inside the for loop, the program checks if the current value of the loop variable s is even or not by using the modulus operator %. If the remainder of s divided by 2 is equal to 0, then s is an even number.
  • If s is even, the program prints the value of s to the console using the println() method of the System.out object.
  • The program then increments the value of s by 1 and returns to the beginning of the for loop.
  • If s is not even, the program skips the println() statement and returns to the beginning of the for loop.
  • The loop continues to execute until the value of s exceeds the limit specified by the user.

Source Code

import java.util.Scanner;
class Even_Numbers
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The Number of Limit : ");
		int l =input.nextInt();
		for(int s=1;s<=l;s++)
		{
			if(s%2==0)
				System.out.println(s);
		}
	}
}

Output

Enter The Number of Limit : 50
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

Example Programs