Check given number is Odd or Even using Java

In this tutorial, We will show you how to write a program to Check given number is Odd or Even using Java Example. The attached program is tested and output is shared in the same post.

CheckOddEven.java

package com.dineshkrish;
import java.util.Scanner;
public class CheckOddEven {
public static boolean checkOddOrEven(int number) {
if (number <= 0) { // Simple Validation
System.out.println("Please provide possitive number");
System.exit(0);
}
if (number % 2 == 0) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find Odd or Even :");
int number = scanner.nextInt(); // Reading the Input
boolean result = checkOddOrEven(number); // Method invocation
if (result) {
System.out.println("The given number is Even");
} else {
System.out.println("The given number is Odd");
}
}
}

Output

Enter the number to find Odd or Even :
10
The given number is Even

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *