In this tutorial we will learn writing the program in Java to check whether a given number is even number or not.
Before start writing Lets know
What is Even and Odd number?
A number is even if it is completely divisible by 2 with 0 remainder. And the number that does not completely divisible by 2 i.e. it will left some remainder known as odd number.
For example:
2, 4, 6 are even numbers because it is completely divisible by 2.
1,3,5,7 are odd numbers because it is not completely divisible by 2.
Problem Statement
For any number that is input by the user, we have to check if it is even or odd.
Case1: If the user inputs number 51
then the output should be ‘odd’.
Case2: If the user inputs a number 100.
then the output should be ‘even’.
Our Logic to find Even and Odd Number
- Our program will take a number as input from the user.
- The input number is checked if it is an even number or not.
- If it is, then print ‘even,’ and if it is not, the result should print ‘odd.’
Let’s discuss how to check if a number is an even number.
An integer when divided by ‘2’ and leaves the remainder 0 is called an even number. We can also define an even number as a number that ends with either 0 or 2 or 4 or 6 or 8 is considered an even number.
All the numbers other than even number is considered odd number. Odd numbers always leave the remainder ‘1’ when divided by ‘2’.
Our program divides the input number by ‘2’. If the remainder is ‘0’, we consider a number an even number; otherwise, it is an odd number.
Algorithm to check number is Even or Odd
Step 1: Start
Step 2: Declare an variable num of integer type.
Step 3: Take an integer input from the user and store it into variable num.
Step 4: if num is divisible by 2
Print ‘even’
else
Print ‘odd’
Step 5: Stop
Program 1: Java program for even and odd
import java.util.*;
public class Main
{
public static void main(String[] args) {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check even or odd ");
num= sc.nextInt();
if(num%2==0){
System.out.println("Given number "+num+" is even");
}else{
System.out.println("Given number "+ num +" is odd");
}
}
}
Output
Enter a number to check even or odd
56
Given number 56 is even
Explanation
- Importing Necessary Package:
- The program imports
java.util.*
, which includes theScanner
class, used for obtaining input from the user.
- The program imports
- Class Definition:
- Defines a public class named
Main
.
- Defines a public class named
- Main Method:
- The
main
method is the entry point of the program, where execution begins.
- The
- Setting Up Scanner:
- A
Scanner
object namedsc
is created to read input from the standard input stream (keyboard).
- A
- Prompting the User:
- The user is prompted to enter a number, with a message printed to the console.
- Reading the Input:
- The program reads the integer input from the user and stores it in the variable
num
.
- The program reads the integer input from the user and stores it in the variable
- Checking Even or Odd:
- The program uses the modulus operator (
%
) to check if the number is divisible by 2 without a remainder. - If
num % 2 == 0
, it prints that the number is even. - Otherwise, it prints that the number is odd.
- The program uses the modulus operator (
- Output:
- Based on the condition, it outputs either “Given number [num] is even” or “Given number [num] is odd”.
Program 2: Even and odd program in java using methods
import java.util.*;
public class Main
{
static void oddEven(int num) {
if(num%2==0){
System.out.println("Given number "+num+" is even");
}else{
System.out.println("Given number "+num+" is odd");
}
}
public static void main(String[] args) {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check even or odd ");
num= sc.nextInt();
oddEven(num);
}
}
Output
Enter a number to check even or odd
54
Given number 54 is even
Explanation
- Import Necessary Libraries:
- Imports
java.util.*
which includes theScanner
class for reading user input.
- Imports
- Class Definition:
- The program is encapsulated within a public class named
Main
.
- The program is encapsulated within a public class named
- Method for Even/Odd Check:
oddEven(int num)
Method: A static method that takes an integernum
as an argument. It uses the modulus operator (%
) to determine if the number is even or odd:- If
num % 2 == 0
, it prints that the number is even. - Otherwise, it prints that the number is odd.
- If
- Main Method:
- Entry Point: The
main
method where the program execution starts. - Scanner Setup: A
Scanner
objectsc
is created to read data from the user via the console. - User Prompt: Outputs a prompt asking the user to enter a number.
- Input Read: The entered integer is read and stored in the variable
num
. - Method Call: Calls the
oddEven
method, passing the user’s inputnum
as an argument to check if it’s even or odd.
- Entry Point: The
- Execution Flow:
- The user is prompted to enter a number.
- The entered number is passed to the
oddEven
method. - The
oddEven
method checks the number and prints the appropriate message based on whether it’s even or odd.
Program 3: Java 8 Program to Check Even and Odd
import java.util.*;
import java.util.stream.*;
public class Main
{
public static void main(String[] args) {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check even or odd ");
num= sc.nextInt();
String result = isEven(num) ? "Even" : "Odd";
System.out.println(num + " is " + result);
}
private static boolean isEven(int number) {
return IntStream.of(number).anyMatch(n -> n % 2 == 0);
}
}
Output
Enter a number to check even or odd
33
33 is Odd
Explanation
- Import Statements:
import java.util.*;
imports theScanner
class necessary for capturing user input.import java.util.stream.*;
imports the classes related to Java streams, specifically used here for checking number properties using stream operations.
- Class Definition:
- The program is encapsulated within a public class named
Main
.
- The program is encapsulated within a public class named
- Main Method:
- Entry Point: The
main
method where program execution begins. - Scanner Setup: A
Scanner
object,sc
, is created to read input from the standard input stream (typically keyboard). - User Prompt: The program prompts the user to enter a number.
- Input Capture: It captures the user’s input as an integer and stores it in the variable
num
. - Conditional Check Using Ternary Operator: Determines the nature of the number (even or odd) by calling the
isEven
method. The result is stored in aString
variableresult
, which is decided by a ternary operator (conditional operator that chooses “Even” or “Odd” based on the boolean outcome ofisEven
). - Output: Prints out whether the number is even or odd by using the
result
string.
- Entry Point: The
- isEven Method:
- Method Definition: A private static method named
isEven
that takes an integernumber
as an argument and returns a boolean. - Stream Operation: Utilizes Java Streams (
IntStream.of(number)
) to create a stream containing the single integernumber
. - Any Match Stream Method: The
anyMatch
method of the stream is used to check if any element of the stream matches the provided condition (n % 2 == 0
). This effectively checks if the number is even. - Return Value: Returns
true
if the number is even, otherwise returnsfalse
.
- Method Definition: A private static method named
- Stream Advantage:
- While the use of streams here is more complex than necessary for checking if a single number is even or odd, it demonstrates how streams can be applied. This approach might be more useful in scenarios where operations on collections of numbers are required.
Conclusion
In this tutorial we have learnt writing java programs to check whether a given number is even or odd. The concepts of even and odd numbers were briefly explained in this tutorial. An even number is divisible by 2 with no remainder, while an odd number leaves a remainder when divided by 2.
We have seen three different approach to write even odd program:
- Basic Program for Even and Odd: This program used a straightforward approach to check whether the input number is even or odd using the modulo operator.
- Program using Methods: The second program introduced a method called
oddEven
to encapsulate the logic for checking even or odd. This modular approach makes the code more readable and reusable. - Java 8 Program: The third program leveraged Java 8 streams and the
anyMatch
method to check if a number is even. This showcased a more modern and concise way of achieving the same result.
Throughout the examples, we are taking user input and determined whether it was even or odd by applying our even odd logic. We have written multiple approaches to implement even and odd number checks in Java, catering to different coding preferences and styles.
Happy coding!