Java program to check whether a given number is even or odd
Write a java program to check whether a number is even or odd
In this program, enter any integer number as an input. Now we check its remainder with modulo operator by two. If remainder is zero the given. If remainder is 1 then the given number is odd. Hence, we show output according to the remainder.
Here is the source code of the java program to check if a given integer is odd or even. The java program is successfully compiled and run on a windows system. The program output is also shown below.
- Program:
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = reader.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
- Output:
Enter a number: 1616 is even--- Second Example ---Enter a number: 2727 is odd
- Visit:
Java program to swap two numbers Here
0 comments