1. Introduction
In this tutorial, You will learn how to write a java program to check a given number is even or odd. This can be done by using a mathematic formula or a different approach. Every number in the world must fall under either an even or odd category. Many developers may provide the solution in different ways but finally, they will tell even or not.
2. Example 1: Checking a number is even or odd using If Else statement
In the below program, Scanner is used to read the number from users' keyboard and stored in int number. The % operator is used to getting the remainder by dividing the given number with 2. If the number is divisible by 2 then produces a remainder value 0 else 1 saying odd.
package com.java.w3schools.blog.java.program.to; import java.util.Scanner; public class EvenOddIfElse { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number :"); int number = scanner.nextInt(); if (number % 2 == 0) { System.out.println(number + " is a even"); } else { System.out.println(number + " is a odd"); } } }
Output:
Enter a number :22 22 is a even
3. Example 2: Checking a number is even or odd using Ternary Operator
The same above program can be done using the ternary operator or conditional operator. The ternary operator takes three arguments. The first argument is a comparison argument like a condition, the second is the result upon a true comparison, and the third is the result upon a false comparison. If it helps you can think of the operator as a shortened way of writing an if-else statement (replacement to if-else). But, the Ternary operator should return value otherwise will get a compile-time error.
package com.java.w3schools.blog.java.program.to; import java.util.Scanner; public class EvenOddTernary { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number :"); int number = scanner.nextInt(); String numberType = (number % 2 == 0) ? "Even" : "Odd"; System.out.println(number + " is " + numberType); } }
Output:
Enter a number :155 155 is Odd
4. Example 3: Find even or odd by Comparing the first digit of the number
As of now, we have seen the example programs using the remainder using the modulo operator. The number is even if remainder is 0 else odd. But there another logic to use and determine the number type. All even numbers must end with 0 or 2 or 4 or 6 or 8 if not ending with these numbers then it is said to be an odd number.
Take the first digit of the number and compare it with all these even digits lists (0, 2, 4, 6, 8). Take a look at the below program.
package com.java.w3schools.blog.java.program.to; import java.util.Scanner; public class EvenOddLastDigitCompare { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number :"); int number = scanner.nextInt(); int firstDigit = number % 10; if (firstDigit == 0 || firstDigit == 2 || firstDigit == 4 || firstDigit == 6 || firstDigit == 8) { System.out.println(number + " is even"); } else { System.out.println(number + " is odd"); } } }
Output:
Enter a number :45 45 is odd
5. Conclusion
In this article, We have seen the possible ways to find the number is even or not.
Covered the following.
- If-Else condition
- Ternary Operator
- The number ends any one of these digits (0,2,4,6,8)
If you have any questions leave at the comments section. Please like the facebook page.
No comments:
Post a Comment
Please do not add any spam links in the comments section.