Check Odd-Even number in java In this article, you’ll learn how to check odd-even number in Java language without giving input from the keyboard or giving input from the keyboard.

Look at in the above diagram:

To check odd-even number we generally use modulo operator, we divide the given number with modulo operator and if get remainder == 0 then we say that the given number is even, otherwise, we say that the given number is odd.

Example 1: write a program in java language to check a number is odd- even without giving input from keyboard

 import java.util.Scanner;   public class OddEven { public static void main(String args[]) { int num=868; if(num%2==0) System.out.println(num+” is an Even number”); else System.out.println(num+” is an Odd number”); } }

 The Output of this program: 868 is an Even num

Example 2: write a program in java language to check a number is odd-even, an input is given by user from the keyboard.

Solution:

 import java.util.Scanner; public class OddEven { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println(“Enter an integer number: “); int num=sc.nextInt(); sc.close(); if(num%2==0) System.out.println(num+” is an Even number”); else System.out.println(num+” is an Odd number”); } }

 The Output of this program: Enter an integer number: 651 651 is an Odd number

Example 3: Write a C++ Program to check odd-even number using the ternary operator, without input from the keyboard.

Solution:

 public class OddEven { public static void main(String args[]) { int num=345; String num2=(num%2==0) ? “Even”:”Odd”; System.out.println(num+” is an ” +num2 +” number”); } }

 The output of this program: 345 is an Odd number

Note: Ternary operator must write in a single statement;

The syntax of the ternary operator or conditional operator:

 (condition) ? statement 1: statement 2;

Note: if the condition is true then statement 1 is executed and if the condition is false statement 2 is executed.

Example 4: Write a C++ Program to check odd-even number using the ternary operator, with input from the keyboard

Solution:

 import java.util.Scanner; public class OddEven { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println(“Enter an integer number: “); int num=sc.nextInt(); sc.close(); String num2=(num%2==0) ? “Even”:”Odd”; System.out.println(num+” is an ” +num2 +” number”); } }

 The output of this program: Enter an integer number: 52 52 is an Even number