Even number

Even number are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.

Logic

This program is similar to the previous one the only thing different here is, here we have to check if the number is completely divisible by 2 or not.

If the number is divisible by 2 then it is an even number, simply print it.

Program

import java.util.Scanner;

public class AllEvenNumbers {
    
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Take input from user.
        System.out.print("Print all even numbers till : ");
        int n = scanner.nextInt();

        System.out.println("\nEven numbers from 1 to " + n + " are : ");

        for(int i = 1; i <= n; i++) {

            // Check for even or not.
            if((i % 2) == 0) {

                System.out.print(i + " ");

            }
        }
    }

}

Output

Print all even numbers till : 50
Even numbers from 1 to 50 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