Java Program to check a number is even or odd using the constructor


In this program, You will learn how to check a number is even or odd using default constructor in Java.


if (number % 2 == 0) { }


Example: How to check a number is even or odd using the constructor in Java

Run it Now
import java.util.Scanner;

class Test {

    int n, i, p = 1;

    Scanner sc = new Scanner(System.in);

    Test() {

        System.out.print("Enter a Number :");
        n = sc.nextInt();
    }

    void check() {

        if (n % 2 == 0) {
            System.out.println("\nNumber is even :" + n);
        } else {
            System.out.println("\nNumber is odd :" + n);
        }

    }
}

class Main {

    public static void main(String args[]) {

        Test obj = new Test();
        obj.check();

    }
}

Output:

Enter a Number :7
Number is odd :7