print
Advertisment
Advertisment

make a program to check number is even or odd

Here is an example:

MyClass.java

public class EvenOddChecker {
    public static void main(String[] args) {
        int number = 10; // Define the number to be checked

        // Check if the number is even or odd
        if (number % 2 == 0) {
            System.out.println(number + " is even."); // If the number is even, print a message to the console
        } else {
            System.out.println(number + " is odd."); // If the number is odd, print a message to the console
        }
    }
}
10 is even.

Since the number defined in the program is 10, which is even, the output will say that 10 is even. If you change the value of number to an odd number, such as 7, the output will say that 7 is odd instead.

Let's go through this program line by line:

  • public class EvenOddChecker: This declares a new public class called EvenOddChecker.
  • public static void main(String[] args): This declares a main method, which is where the program starts executing. It takes an array of strings as an argument, which is not used in this program.
  • int number = 10;: This declares an integer variable called a number and initializes it with the value 10. This is the number that we will check to see if it is even or odd.
  • if (number % 2 == 0) {: This starts an if statement that checks whether the remainder of the number divided by 2 is equal to 0. If it is, then the number is even.
  • System.out.println(number + " is even.");: If the number is even, we print a message to the console that says the number is even. The println method prints a string to the console and adds a new line at the end.
  • } else {: If the number is not even, we enter the else block.
  • System.out.println(number + " is odd.");: This prints a message to the console that says the number is odd.
  • }: This ends the if statement.

When you run this program, it will check whether the number defined in the number is even or odd and print a message to the console accordingly.

Advertisment
Advertisment
arrow_upward