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) { 
     //statement
}

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

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("Number is even :" + n);
        } else {
            System.out.println("Number is odd :" + n);
        }

    }
}

class Main {

    public static void main(String args[]) {

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

    }
}

Output:

Enter a number:14
Number is even :14

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.