Java program to check whether a number is even or odd
www.igc.aeditfiom
Here's a Java program to check whether a number is even or odd:
public class Main { public static void main(String[] args) { int number = 42; if (number % 2 == 0) { System.out.println(number + " is even"); } else { System.out.println(number + " is odd"); } } }
In this program, we first initialize an int
variable number
with the value 42
.
We then use the modulus operator %
to calculate the remainder when number
is divided by 2
. If the remainder is 0
, number
is even; otherwise, it is odd. We use an if
statement to check the remainder and print out the appropriate message.
The output of this program will be:
42 is even
You can replace 42
with any other integer value to check whether it is even or odd.