Java Program to Check Even or Odd Number using If-Else Statement | Ternary Operator | Function
Java program to check even or odd number using if-else statement, ternary operator & function.
In this article, you will learn how to make a java program to check even or odd number using an if-else statement, ternary operator & function.
Example
The input number is odd.
What are even odd numbers?
There is if any integer number divided by 2 and its reminder equals zero then it is an even number and if the remainder is greater than zero then it is an odd number.
Samples of Even-Odd Numbers
You should have knowledge of the following topics in java programming to understand these programs:
- Java
java.util.Scanner
package - Java
main()
method - Java
If-else
statement - Java Ternary operator
- Java Functions
- Java Switch Case
In this article, we solve this problem in four methods:
Source Code
// Java Program to Check Even or Odd Number using If-else Statement
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x;
System.out.println ("Enter an integer number to check:\n");
x = in.nextInt();
if (x % 2 == 0) {
System.out.println ("The input number is even.\n");
} else {
System.out.println ("The input number is odd.\n");
}
}
}
Output
Enter an integer number to check:
44
The input number is even.
Explanation
In this given program, we have taken input 44
from the user via system input then we divided this input number by 2
with its remainder.
If the remainder is zero It's an even number otherwise It's an odd number.
Java Program to Check Even or Odd Number using Conditional Operator
// Java Program to Check Even or Odd Number using Conditional Operator
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x;
System.out.println ("Enter an integer number to check:\n");
x = in.nextInt();
x = x % 2 == 0 ? 1 : 0;
if (x == 1) {
System.out.println ("The input number is even.\n");
} else {
System.out.println ("The input number is odd.\n");
}
}
}
Output
Enter an integer number to check:
45
The input number is odd.
Java Program to Check Whether a Number is Even or Odd using Function
// Java Program to Check Whether a Number is Even or Odd using Function
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x;
System.out.println ("Enter an integer number to check:\n");
x = in.nextInt();
IsEvenNumber(x);
}
public static void IsEvenNumber(int x) {
if (x % 2 == 0) {
System.out.println ("The input number is even.");
} else {
System.out.println ("The input number is odd.");
}
}
}
Java Program to Check Even or Odd using Switch Case
// Java Program to Check Even or Odd using Switch Case
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x;
System.out.println ("Enter an integer number to check:");
x = in.nextInt();
x = x % 2 == 0 ? 1 : 0;
switch(x) {
// If value of "x" is 1 the input number is "Even"
case 1:
System.out.println ("\nThe input number is even.");
break;
// If value of "x" is 0 the input number is "Even"
case 0:
System.out.println ("\nThe input number is odd.");
break;
default:
System.out.println ("\nInvalid case given!");
break;
}
}
}
Output
Enter an integer number to check:
46
The input number is even.