Dremendo Tag Line

Check a number is even or odd in Java

if else - Question 1

In this question, we will see how to check if a number is an even or odd number in Java programming using the if else statement. To know more about if else statement click on the if else statement lesson.

Q1) Write a program in Java to input a number and check it is an even or odd number. An even number is a number which is divisible by 2 and odd number is a number which is not divisible by 2.

Program

import java.util.Scanner;

public class Q1
{
    public static void main(String args[])
    {
        int n;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number ");
        n=sc.nextInt();
        if(n%2==0)
        {
            System.out.print("Even Number");
        }
        else
        {
            System.out.print("Odd Number");
        }
    }
}

Output

Enter a number 18
Even Number
video-poster