Java: How to Find Even or Odd Numbers

Determining whether a number is even or odd is a basic programming task that helps in solving various computational problems. In this tutorial from The Coding College, we’ll explore how to find whether a number is even or odd in Java.

Understanding Even and Odd Numbers

  • Even Numbers: Divisible by 2 without a remainder (e.g., 2, 4, 6).
  • Odd Numbers: Leave a remainder of 1 when divided by 2 (e.g., 1, 3, 5).

Formula:

Example Programs

1. Basic Program

import java.util.Scanner;

public class EvenOddCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input from user
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();

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

Output Example:

Enter a number: 7  
7 is an odd number.  

Enter a number: 10  
10 is an even number.  

2. Using a Method

To improve reusability, let’s create a method that checks if a number is even or odd.

public class EvenOddCheck {
    // Method to check even or odd
    public static String checkEvenOdd(int number) {
        return (number % 2 == 0) ? "even" : "odd";
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int number = scanner.nextInt();

        // Call the method and display result
        System.out.println(number + " is an " + checkEvenOdd(number) + " number.");
    }
}

3. Checking Multiple Numbers

If you need to check a list of numbers, you can use loops:

import java.util.Scanner;

public class EvenOddCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("How many numbers do you want to check? ");
        int count = scanner.nextInt();

        for (int i = 0; i < count; i++) {
            System.out.print("Enter number " + (i + 1) + ": ");
            int number = scanner.nextInt();

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

Real-Life Applications

  1. Game Development: Identify even or odd moves or scores.
  2. Data Analytics: Segregate datasets into even and odd categories.
  3. Cryptography: Perform operations based on even or odd conditions.

Best Practices

  • Input Validation: Ensure the input is a valid integer.
  • Efficiency: Use bitwise operations (number & 1) for faster performance in large datasets.
  • Reusability: Create a utility class for even-odd checks.

Conclusion

Finding whether a number is even or odd is a simple yet important concept in programming. With this tutorial, you now have multiple ways to implement this functionality in Java.

For more coding tutorials and practical guides, explore The Coding College and enhance your coding journey!

Leave a Comment