Mar 29, 2021

Write a java program to print given number is Even or Odd

Ex: Write a java program to print given number is Even or Odd. How to write a program to check whether the given number is even or odd in java. write java program to check number entered by user is even or odd. write a program to check whether the given number is even.

Program:

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

Share This:    Facebook Twitter

0 comments:

Post a Comment

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