Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java Odd Even Program

by suresh

Write a Java Odd Even Program to show you, How to check whether the given number is Odd or Even number using If Statement and Conditional Operator with example. If the number is divisible by 2, it as even number, and the remaining (not divisible by 2) are odd numbers.

The Java Programming has a % (Module) Arithmetic Operator to check the remainder. If the remainder is 0, then the number is even otherwise, an odd number.

Java Odd Even Program using IF Condition

This Java program allows the user to enter any integer value. Then this Java program checks whether that number is even or odd using Java If statement.

//Java Odd Even Program using If Else Statement
package SimpleNumberPrograms;

import java.util.Scanner;

public class EvenorOddUsingIf {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter any integer Value: ");
		Number = sc.nextInt();
		
		if (Number % 2 == 0) {
			System.out.println("\n You have entered EVEN Number");
		}
		else {
			System.out.println("\n You have entered ODD Number");
		}
	}
}

ANALYSIS

Within the Java even or odd example, the following statement will ask the user to Enter any integer value to check whether it is Even or Odd.

System.out.println("\n Please Enter any integer Value: ");

The below statement will assign the user entered value to a number variable.

Number = sc.nextInt();

In the Next line, We declared the If Else statement to check even or odd in Java

if (Number % 2 == 0) {

As we all know, the number that is completely divisible by 2 is even number. So, we used the If condition to check whether the remainder of the number divided by 2 is exactly equal to 0 or not.

  • If the Java If statement is True, the statement inside the If block will print. That is Even number
  • If the condition is False, the Java Programming Else block statement will print. That is Odd number

OUTPUT

We have entered the value as 48 to check, and the Java program is returning the even number as output

Java Odd Even Program using If

Java Odd Even Program using Conditional Operator

This Java program checks whether that number is even or odd using the Java Ternary operator. Let’s see the example.

//Java Odd Even Program using Conditional Operator
package SimpleNumberPrograms;

import java.util.Scanner;

public class EvenorOddUsingConditional {
 private static Scanner sc;
 
 public static void main(String[] args) {
 int Number;
 sc = new Scanner(System.in); 
 System.out.println("\n Please Enter the First integer Value: ");
 Number = sc.nextInt();
 
 System.out.println((Number%2)==0? "\n You have entered EVEN Number":
                 "\n You have entered ODD Number");
 }
}

OUTPUT

From the below Java Odd Even Program screenshot, you can observe that we entered 19 as input. As you can see, it is returning Odd Number as Output

Java Odd Even Program using Conditional Operator

You can observe from the above Java Odd Even Program code that, we used if condition to check whether the remainder of the given integer divisible by 2 is 0 or not and print as per the result.

Let us enter 28 to check the other output

Java Odd Even Program using Conditional Operator 2

Java Odd Even Program using Methods

In this Java program, we are using the same steps that we followed in our first example. However, in this Java program, we separated the logic to find even or odd and placed in the separate method.

package SimpleNumberPrograms;

import java.util.Scanner;

public class EvenorOddUsingMethods {

	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter the First integer Value: ");
		Number = sc.nextInt();
		
		evenOrOdd(Number);

	}	
	public static void evenOrOdd(int Number) {
		if (Number % 2 == 0) {
			System.out.println("\n You have entered EVEN Number");
		}
		else {
			System.out.println("\n You have entered ODD Number");
		}
	}
}

OUTPUT

Java Odd Even Program using Methods

ANALYSIS

In this Java Odd Even Program, If you observe the following statement, we are calling the evenOrOdd method

evenOrOdd(Number);

Let us see the code inside the evenOrOdd method

public static void evenOrOdd(int Number) {
	if (Number % 2 == 0) {
		System.out.println("\n You have entered EVEN Number");
	}
	else {
		System.out.println("\n You have entered ODD Number");
	}
}

From the above code snippet, you can observe that this method accepts one argument of integer type. Within the function, we are performing the same operation that we explained in our previous example.

Java Odd Even Program using OOPs

In this program, we are dividing the code using the Object-Oriented Programming. To do this, First, we will create a class that holds two methods.

package SimpleNumberPrograms;

public class EvenOrOdd {
	int x;
	
	public void evenOrOdd() {
		if (x % 2 == 0) {
			System.out.println("\n First Method: You have entered EVEN Number");
		}
		else {
			System.out.println("\n First Method: You have entered ODD Number");
		}
	}	
	public int evenOrOddAgain(int Number) {
		if (Number % 2 == 0) {
			return 1;
		}
		else {
			return 0;
		}
	}
}

Within the Main Java Odd Even program, we will create an instance of the above-specified class and call the methods to find even or odd in Java

package SimpleNumberPrograms;

import java.util.Scanner;

public class EvenorOddUsingClass {
 private static Scanner sc;
 public static void main(String[] args) {
 int Number;
 sc = new Scanner(System.in); 
 System.out.println("\nPlease Enter the integer Value to check Even or Odd: ");
 Number = sc.nextInt();
 
 EvenOrOdd eo = new EvenOrOdd();
 eo.x = Number;
 
 eo.evenOrOdd();
 if (eo.evenOrOddAgain(Number) != 0) {
 System.out.println("\n Second Method: You have entered EVEN Number");
 }
 else {
 System.out.println("\n Second Method: You have entered ODD Number");
 }
 }
}

OUTPUT

Java Odd Even Program 3

ANALYSIS

Java Odd Even Program EvenOrOdd Class Analysis:

  • First, we declared a function evenOrOdd with zero arguments. Within the function, we used the If statement to check whether the given number is perfectly divisible by two or not. If it is True, we are printing Even Number statement otherwise, Odd Number statement using System.out.println statement.
  • Next, we declared an integer function evenOrOddAgain with one argument. Within the function, we used the If statement to check whether the given number is perfectly divisible by 2 or not. If it is True, we are returning Value 1 otherwise, 0.

Main Class Analysis:

In this Java Odd Even Program, First we created an instance / created an Object of the EvenOrOdd Class

EvenOrOdd eo = new EvenOrOdd();

Next, we are assigning the user entered values to the EvenOrOdd Class variables.

eo.x = Number;

Next, we are calling the evenOrOdd method. Note, this is the first method that we created with the void keyword, and this method will check for even or odd and print the output from the EvenOrOdd Class itself.

eo.evenOrOdd();

Next, Within the If statement, we are calling the evenOrOddAgain method and checking whether the return value is Not Equal to Zero or Not. If the condition is True, the statement inside the If block will execute. Otherwise, it will run the statement inside the else block.

Placed Under: Java Programs

  • Java Hello World Program
  • Java Add Two Numbers Program
  • Java Compound Interest Program
  • Java Number Divisible by 5 & 11
  • Java Cube of a Number Program
  • Java Print Even Numbers 1 to N
  • Java GCD of Two Numbers
  • Java LCM of Two Numbers
  • Java Largest of Two Numbers
  • Java Largest of Three Numbers
  • Java Print Multiplication Table
  • Java Odd Numbers from 1 to N
  • Java Odd Even Program
  • Java find +Ve or -Ve number
  • Java Power of a Number Program
  • Java Calculate Profit or Loss
  • Java Print 1 to 100 without Loop
  • Java Quadratic Equation roots
  • Java Square of Number Program
  • Java Simple Interest Program
  • Java Sum of Even Numbers
  • Java Sum of Odd numbers
  • Java Sum of Even & Odd Number
  • Java find Total ,Average & Percentage of 5 Subjects
  • Java ASCII Value of a Character
  • Java ASCII Value of all Characters
  • Java String Comparison program
  • Java Area of Circle Program
  • Java Area Of Triangle Program
  • Java Area Of Trapezoid Program
  • Java Area of Equilateral Triangle
  • Java Area of Rectangle Program
  • Java Area of Right angle triangle
  • Java Volume & Surface of Sphere
  • Java Volume & Surface of Cone
  • Java Volume & Surface of Cuboid
  • Java Volume & Surface of Cube
  • Java Cylinder Volume & Surface
  • Java Math Library
  • Java Armstrong Number Program
  • Java Perfect Number Program
  • Java Palindrome Program
  • Java Count Digits in a Number
  • Java Calculate Electricity Bill
  • Java Find Factors of a Number
  • Java Factorial Program
  • Java Fibonacci Series program
  • Java find First Digit of a Number
  • Java find Last Digit of a Number
  • Java First & Last Digit of Number
  • Java Generic Root of Number
  • Java Natural Numbers in Reverse
  • Java Natural Numbers from 1 to N
  • Java Find Sum of Prime Numbers
  • Java Print Prime Numbers 1 to N
  • Java Prime Number Program
  • Java Reverse a Number Program
  • Java Strong Number Program
  • Java Sum of N Natural Numbers
  • Java Sum of Digits Program
  • Java Swap Two Numbers
  • Java Leap Year Program
  • Java Number of Days in a Month
  • Java Array
  • Java Array Arithmetic Operations
  • Java Program to Copy an Array
  • Java Count Array Duplicates
  • Java count even number in array
  • Java count odd numbers in Array
  • Java Count Even & Odds in Array
  • Java Count Negative Array Items
  • Java Count Positive Array Items
  • Java Count Array +Ve & -Ve num
  • Java Delete Array Duplicates
  • Java Large & Small Array Item
  • Java Largest Array Number
  • Java Print Array Elements
  • Java Print Positive Array Items
  • Java Print Negative Array Items
  • Java Smallest Array Number
  • Java Second Largest Array Item
  • Java Print Unique Array Items
  • Java +Ve & -Ve in separate array
  • Java Sort Array in Descending
  • Java Sort Array in Ascending
  • Java Sum of Array Elements
  • Java Sum of Array Even Numbers
  • Java Sum of Array Odd Numbers
  • Java Sum of Array Even and Odd
  • Java Swap Arrays without temp
  • Java Matrix Introduction
  • Java Print Matrix items
  • Java Check 2 Matrices are Equal
  • Java Matrix Arithmetic operation
  • Java Matrix Addition
  • Java Matrix Multiplication
  • Java Find Matrix Determinant
  • Java Identity Matrix
  • Java Interchange Matrix diagonal
  • Java Matrix Lower Triangle
  • Java Matrix Lower Triangle Sum
  • Java Matrix Upper Triangle
  • Java Matrix Upper Triangle Sum
  • Java Scalar Matrix Multiplication
  • Java Sparse Matrix
  • Java Sum of Matrix Diagonal
  • Java Sum of Matrix Opp Diagonal
  • Java Sum of each Matrix Row
  • Java Sum of each Matrix Column
  • Java Sum of Matrix row & column
  • Java Symmetric Matrix
  • Java Transpose Matrix
  • Java Rectangle Star Pattern
  • Java Square Star Pattern
  • Java Hollow Box Number Pattern
  • Java Box Number Pattern of 1, 0
  • Java Square Number Pattern
  • Java Print Floyd’s Triangle
  • Java Print String Characters
  • Java String replace 1st Char Occ
  • Java String replace last Char occ
  • Java String remove 1st Char Occ
  • Java string remove last Char Occ
  • Java String remove 1st, last char
  • Java String remove all char occur
  • Java String Find 1st Char Occur
  • Java String find all char occur
  • Java String Find Last Char Occur
  • Java String Print 1st & last Char
  • Java String Chars ASCII values
  • Java Program to Concat Strings
  • Java String Characters count
  • Java Count Total Char Occur
  • Java Max Occur String Character
  • Java Toggle String Characters
  • Java Frequency of each Char
  • Java Count Vowels & Consonants
  • Java Count alph digits, special
  • Java Palindrome String
  • Java Program to Reverse a String
  • Java Min Occur String character
  • Java Convert Lower to Upper
  • Java Convert Upper to Lower
  • Java Count total String words
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy