Write a program to check a number is odd or even?

Problem:- Write a program to check whether a given number is even or odd using Java?
Solution:- As we know if the number is divisible by 2 with remainder 0 is called even else odd number.
Example:- If we divide 4 by 2 we will get the remainder 0 because 4 is divisible by 2. So, 4 is an even number.
Java code:- 

package com.codeforsolution.java.logical;

import java.util.InputMismatchException;
import java.util.Scanner;

public class EvenOddCheck {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number to check even or odd");
int num;
try {
num = s.nextInt();
if (num % 2 == 0) {
System.out.println(num + " is an even number");
} else {
System.out.println(num + " is odd number");
} catch (InputMismatchException e) {
System.out.println("Not a valid Integer");


Enter a number to check even or odd
20 is an even number
Post a Comment (0)
Previous Post Next Post