Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

Java - check if number is odd or even

0 contributions
7 points

If a number is divisible by two then it is even, otherwise, it is odd.
To find reminder in java we use modulo operator %.

Even numbers always end with a digit of 0, 2, 4, 6 or 8
Odd  numbers always end with a digit of 1, 3, 5, 7 or 9

Examples of even numbers:
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26

Examples of odd numbers:
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27

public class JavaCheckIfNumberIsOddOrEven {

    public static void oddOrEven(int no) {
        if (no % 2 == 0) {
            System.out.println(no + " is even number");
        } else {
            System.out.println(no + " is odd number");
        }
    }

    public static void main(String[] args) {
        oddOrEven(4); // 4 is even number
        oddOrEven(5); // 5 is odd number
    }
}

Output:

4 is even number
5 is odd number

Reference:

Wiki - Parity
Modulo in java - syntax, how to use it with examples

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more