Here, you will learn to check the number taken from the user as an input using a scanner is Odd or Even. After checking display the result accordingly.
The concept of checking the number using the Modulus operator. First, calculate the number by modulus by 2, and if the Number mod 2 is equal to 0 then it is an even number else not.
Flowchart for odd or even number
Java Program to Check whether the Number is Odd or Even
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //check the number for odd and even import java.util.Scanner; public class CheckEvenOdd { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter an integer: "); int num = sc.nextInt(); if(num % 2 == 0) System.out.println(num + " is even"); else System.out.println(num + " is odd"); } } |
Output:
Enter an integer: 43
43 is odd