Java Program to Check Whether a Number is Odd or Even

Java Program to Check Whether a Number is Odd or Even

What is Odd and Even Numbers ?

An even number is one that can be divided into two equal parts, such as 2 or 4, or one that can be divided by 2 and leave a remainder of 0. Examples of even numbers include 2, 4, 6, 8, and 10, among others.
And an odd number is a number that cannot be divided into two equal parts, such as 3, 5, 7, 9, etc .

Even and Odd Number :

  1. Odd numbers: An odd number is any natural number that can be written as (2n + 1), where n is any natural number, i.e., a positive integer.
  2. Even numbers: An even number is any natural number that can be written as 2n, where n is any natural number, i.e., a positive integer.

Steps to check weather a number is Odd or Even.

  1. Set the number you want to check as an integer variable.
  2. When dividing by 2, determine the remainder using the modulo operator (%).
  3. The number is even if the outcome of the modulo operation is 0.
  4. The number is odd if the outcome of the modulo operation is not 0.
  5. Indicate whether the number is odd or even by printing a message.

Pseudo code for the above algorithm :

public static void main(String[] args) {
    int number = 4;
    int result = number % 2;

    if (result == 0) {
        System.out.println(number + " is even");
    } else {
        System.out.println(number + " is odd");
    }
}

Example : Using if else Statement 

Run

public class Main {
    public static void main(String[] args) {
        int number = 4;

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

Output :

4 is even

Explanation : 

In this Example the integer variable number is declared and set to 4 in the main method. The modulo operator (%) is used in the if…else statement to check the remainder after dividing the number by 2. The message “4 is even” is printed if the outcome is 0, which indicates that the number is even. The message “4 is odd” is printed if the outcome is not zero, which indicates that the number is odd.

Example : Using ternary operator

Run

public class Main {
    public static void main(String[] args) {
        int number = 4;

        String message = (number % 2 == 0) ? number + " is even Number" : number + " is odd Number";
        System.out.println(message);
    }
}

Output :

4 is even Number

Explanation :

In this example the main method declares an integer variable number and sets it to 4. When a number is divided by two using the modulo operator (%), the ternary operator?: checks the remainder. If the outcome is 0, the number is even, and the message “4 is even” is assigned to the message variable. If the result is greater than zero, the number is odd, and the message “4 is odd” is assigned to the message variable. The message variable’s value is then printed to the console..

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription