Java Program To Print Even Numbers From 1 To 100

In this program, we will learn to code the Java Program To Print Even Numbers From 1 To 100. Let’s understand How to print all the even numbers from 1 to 100 in Java Programming Language. In previous programs, we have learned to code the Java Program to Check Even or Odd Numbers.

Let’s see the code of the Java Program To Print Even Numbers From 1 To 100.

Java Program To Print Even Numbers From 1 To 100

import java.util.*;

public class Main {

  public static void main(String[] args) {
    
    System.out.println("Even Numbers from 1 to 100 are :");
    for(int num=1 ; num <= 100 ; num++){
      if(num % 2 == 0){
        System.out.print(""+num+" ");
      }
    }

  }
}

Output

Even Numbers from 1 to 100 are :
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 

How Does This Program Work ?

    for(int num=1 ; num <= 100 ; num++){
      if(num % 2 == 0){
        System.out.print(""+num+" ");
      }
    }

In this program, we have a loop statement from 1 to 100 which checks for every value of i.

If i % 2 == 0 is true, then that value of i is an even number and we will print it.

This loop continues until i becomes 100.

Java Program To Print Even Numbers From 1 TO N

import java.util.*;

public class Main {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter maximum range: ");
    int N = sc.nextInt();
    
    System.out.println("Even Numbers from 1 to "+N+" are :");
    for(int num=1 ; num <= N ; num++){
      if(num % 2 == 0){
        System.out.print(""+num+" ");
      }
    }

  }
}

Output

Enter maximum range: 21
Even Numbers from 1 to 21 are :
2 4 6 8 10 12 14 16 18 20 

Java Program To Print Even Numbers from 1 To N Without Using If Statement

import java.util.*;

public class Main {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter maximum range: ");
    int N = sc.nextInt();
    
    System.out.println("Even Numbers from 1 to "+N+" are :");
    for(int num=2 ; num <= N ; num+=2){
      
        System.out.print(""+num+" ");
      
    }

  }
}

Output

Enter maximum range: 51
Even Numbers from 1 to 51 are :
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 

Conclusion

I hope after going through this post, you understand Java Program To Print Even Numbers From 1 To 100. We discussed two approaches in this post. We also learned to code the Java Program To Print Even Numbers From 1 TO N.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *