Write a Java Program to Print Given Number is Even or Odd

In this example, You will learn how to print given number is even or odd using java. In below program we will use if-else statements to check given number is even or odd.


  Java Program to Print given Number is Even or Odd:


import java.util.Scanner;/*util.Scanner is a class which is used to get input from user*/

public class evenodd {

    public static void main(String[] args) {

        int num;

        Scanner sc = new Scanner(System.in);/*It will create a new object of type Scanner*/

        System.out.println("Ener the number: ");
        num = sc.nextInt();/*It Scans the next token of the input as a int */

        if(num%2 == 0) {
            System.out.println("Given number is Even");
        }
        else {
            System.out.println("Given number is Odd");
        }
    }
}

Above program shows the following Output:
Output 1:
 
Enter the number: 66
Given number is Even

Output 2:
Enter the number: 77
Given number is Odd

No comments:

Post a Comment

If you have any doubts, please discuss here...👇