"KEEP LEARNING, DON'T STOP."
"KEEP LEARNING, DON'T STOP."
In this section you will learn how to find whether a number is even or odd in Java. We will use if else condition and small mathematic operation to check the even or odd number and we will show the result on output screen. In this program we are taking the input from user.
In this program we are taking the input from the user. We are using scanner function to take the input from user. To take the input from user we will import scanner package in our program.
Scanner function is use to take the input from user in java.
2, 4, 6, 8, 10, 12, etc.
1, 3, 5, 7, 8, 9, 11, etc.
import java.util.Scanner; class EvenOdd { public static void main(String arg[]) { int n; Scanner sc = new Scanner(System.in); System.out.println("ENTER A NUMBER:-"); n = sc.nextInt(); if(n%2 == 0) { System.out.println("ENTER NUMBER IS EVEN."); } else { System.out.println("ENTER NUMBER IS ODD."); } } }
ENTER A NUMBER:- 5 ENTER NUMBER IS ODD.
Advertisment