java

Write a Java Program to Check Whether an Entered Number is Odd or Even

In this tutorial, we are going to see how to write a Java program to check whether an entered number is odd or even. If the number is divisible by two, then it is even, otherwise odd. We use the modulo (%) operator to find the remainder. For an even number, it is 0 when divided by two, and 1 for an odd number.
 

Java Program to Check Whether an Entered Number is Odd or Even:
import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  {
    int nbr;
    System.out.print("Enter an integer : ");
    Scanner sc = new Scanner(System.in);
    nbr = sc.nextInt();

    if (nbr % 2 == 0)
      System.out.println("Even number.");
    else
      System.out.println("Odd number.");
  }
}

Output:

Enter an integer : 2
Even number.

 
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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