Java Program to check a number is even or odd using class and object
In this program, You will learn how to check a number is even or odd using class and object in java.
if (n % 2 == 0) {
//statement
}
Example: How to check a number is even or odd using class and object in java.
import java.util.Scanner;
class Test {
int n, i, p = 1;
Scanner sc = new Scanner(System.in);
void input() {
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.input();
obj.check();
}
}
Output:
Enter a number:4
Number is even:4