In this article, we will write a simple Java program to check whether passed/input number is Even or Odd
Find Even and Odd numbers :
- Using traditional for–loop and if–else construct
- Using Java 8 Stream.filter() method and and if-else construct
- Using Java 8 Stream.forEach() method and List
- Using Java 8 Stream.forEach() method and StringBuilder
1. Using traditional for-loop and if-else construct :
- We will evaluate the given number using if-else statements,
- If number is divided by 2 and remainder is Zero then number is Even
- Else number is Odd
CheckNumberEvenOrOddOrZero.java
package in.bench.resources.numbers;
public class CheckNumberEvenOrOddOrZero {
// main method
public static void main(String[] args) {
checkNumberIsEvenOrOdd(19); // odd
checkNumberIsEvenOrOdd(0); // zero
checkNumberIsEvenOrOdd(20); // even
}
/**
* This method prints whether number is Odd or Even or Zero
*
* @param number
*/
public static void checkNumberIsEvenOrOdd(int number) {
if(number%2 == 0) {
// number is Even
System.out.println(number + " is an Even number !!");
}
else {
// number is Odd
System.out.println(number + " is an Odd number !!");
}
}
}
Output:
19 is an Odd number !!
0 is an Even number !!
20 is an Even number !!
2. Using Java 8 Stream.filter() method and if-else construct :
- Using Java 8 Stream, it is very easy to iterate and filter even numbers in a single expression/statement
- Note: if same stream used to find to Odd numbers then java.lang.IllegalStateException exception is thrown stating “stream has already been operated upon or closed“
FindOddEvenNumber.java
package in.bench.resources.java8.numbers;
import java.util.stream.IntStream;
public class FindOddEvenNumber {
public static void main(String[] args) {
// Create IntStream from 1 to 50
IntStream intStream = IntStream.range(1, 50);
// print even numbers
System.out.println("Even numbers :- ");
intStream
.filter(num -> (num % 2 == 0))
.forEach(even -> System.out.print(even + " "));
// print odd numbers
// System.out.println("Odd numbers :- ");
// intStream
// .filter(num -> (num % 2 != 0))
// .forEach(odd -> System.out.print(odd + " "));
}
}
Output:
Even numbers :-
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
3. Using Java 8 Stream.forEach() method and List :
- Using Java 8 Stream, it is very easy to iterate using Stream.forEach() method and build logic for even/odd numbers and store the results into a List as shown in the below illustration
FindOddEvenNumber.java
package in.bench.resources.arrays.find.number;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public class FindOddEvenNumber {
public static void main(String[] args) {
// 1. declare list to store odd/even numbers
List<Integer> oddNumbers = new ArrayList<>();
List<Integer> evenNumbers = new ArrayList<>();
// 2. Create IntStream from 1 to 50
IntStream intStream = IntStream.range(1, 50);
// 3. Check & print even/odd numbers
intStream
.forEach(n -> {
if(n % 2 == 0) {
evenNumbers.add(n);
} else {
oddNumbers.add(n);
}
});
// 4. print odd/even numbers to the console
System.out.println("Even numbers :- \n" + evenNumbers);
System.out.print("\nOdd numbers :- \n" + oddNumbers);
}
}
Output:
Even numbers :-
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
Odd numbers :-
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
4. Using Java 8 Stream.forEach() method and StringBuilder :
- This illustration is very same like the previous example 3 except that instead of storing even/odd numbers in List append those numbers in separate StringBuilder object
FindOddEvenNumber2.java
package in.bench.resources.arrays.find.number;
import java.util.stream.IntStream;
public class FindOddEvenNumber2 {
public static void main(String[] args) {
// 1. declare StringBuilder to store odd/even numbers
StringBuilder sbdEven = new StringBuilder();
StringBuilder sbdOdd = new StringBuilder();
// 2. Create IntStream from 1 to 50
IntStream intStream = IntStream.range(1, 50);
// 3. Check & print even/odd numbers
intStream
.forEach(n -> {
if(n % 2 == 0) {
sbdEven.append(n).append(" ");
} else {
sbdOdd.append(n).append(" ");
}
});
// 4. print odd/even numbers to the console
System.out.println("Even numbers :- \n" + sbdEven.toString().trim());
System.out.print("\nOdd numbers :- \n" + sbdOdd.toString().trim());
}
}
Output:
Even numbers :-
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
Odd numbers :-
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Related Articles :
- Java – To print first N natural numbers
- Java – Swapping two numbers without temporary variable
- Java – Swapping two numbers using third or temporary variable
- Java – Check whether the given number is Armstrong number or Not ?
- Java – Check whether number is Positive or Negative or Zero ?
- Java – Check whether number is Even or Odd ?
- Java – How to Reverse a Number in different ways ?
- Java – Check whether number is Prime or Not ?
- Java – Print Prime numbers between specified range or interval
- Java – How to find/remove first and last digit of a number ?
- Java – How to generate Fibonacci numbers using Stream ?
- Java – How to convert Roman letter/numbers to Integer ?
- Java – How to convert Celsius to Fahrenheit and vice-versa ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html
Happy Coding !!
Happy Learning !!