Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
Now Reading
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
Before programmers can start writing complex polynomial functions and go on to develop applications, embedded systems, and big data processing in Java, they must master the basics of programming. Learning the even odd program in Java is amongst the most fundamental skills students study when coding with Java. This extensive tutorial on odd even program in Java will help beginners apply these basic functions to various algorithms and mathematical equations later on.
Among the different classifications of natural numbers, even and odd is one fundamental classification. Any natural number completely divisible by 2, i.e., generating a remainder 0, is even. Some examples are ⎯ 64, 42, and 88.
The remaining numbers are odd since they leave a remainder of 1 when divided by 2. Some examples of odd numbers are ⎯ 13, 91, and 67.
There are various methods to check if a number is odd or even in Java. Some methods to check even and odd numbers include Brute Force Naive Approach, Bitwise operators like OR, AND, XOR, LSB (Least Significant Bit), and ternary operators.
public class Main
{
public static void main(String[] args) {
int n= 51;
//checking whether the number is even or odd
if (n% 2 == 0)
System.out.println(n + " is Even");
else
System.out.println(n + " is odd");
}
}
This Java program uses “public static void main(String[] args)” main method to determine whether the user input variable ‘n’ is odd or even. We have assigned ‘n’ the value 51. If “(n% 2 == 0)” the remainder is equal to zero, the number is even. Finally, “System.out.println()” is used to print the result to the console. The "51 is odd" message is the printed result in this case.
//Java Program to Check if Given Integer is Odd or Even
// Using Bitwise OR
// Importing required classes
import java.util.*;
import java.util.Scanner;
// Main class
public class even_odd {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer variable
// to be checked
//int n = 101;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n= sc.nextInt();
System.out.println("number is: " + n);
// Condition check
// if n|1 if greater than n then this number is even
if ((n | 1) > n) {
// Print statement
System.out.println("Number is Even");
}
else {
// Print statement
System.out.println("Number is Odd");
}
}
}
Here, the main class “even_odd” is declared with the public access modifier and prompts the user to enter a number using “System.out.println("Enter a number");”. The input number is stored in an integer variable “n” using “int n= sc.nextInt();”. The program checks if it is even or odd using the bitwise OR operator “|.” If the condition is true, the program prints "Number is Even" and ends after printing the result.
//Java Program to Check if Given Integer is Odd or Even
// Using Bitwise AND
// Importing required classes
import java.util.*;
import java.util.Scanner;
// Main class
public class even_odd {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer variable
// to be checked
//int n = 101;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n= sc.nextInt();
System.out.println("number is: " + n);
// Condition check
// if n|1 if greater than n then this number is even
if ((n & 1) == 1) {
// Print statement
System.out.println("Number is Odd");
}
else {
// Print statement
System.out.println("Number is Even");
}
}
}
This even odd program in Java using Scanner class uses the bitwise “AND” operator to check if the least significant bit is set to 1 or 0. Variable “n” stores the user input number, which in this case is 7. If the least significant bit returns 1, it is odd; if it returns 0, it is even. After displaying the “Number is Odd” or “Number is Even” message, the program terminates.
//Java Program to Check if Given Integer is Odd or Even
// Using Bitwise X-OR
// Importing required classes
import java.util.*;
import java.util.Scanner;
// Main class
public class even_odd {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer variable
// to be checked
//int n = 101;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n= sc.nextInt();
System.out.println("number is: " + n);
// Condition check
// if n|1 if greater than n then this number is even
if ((n ^ 1) == n + 1) {
// Print statement
System.out.println("Number is Even");
}
else {
// Print statement
System.out.println("Number is Odd");
}
}
}
The program imports the necessary classes and creates a “Scanner” object "sc" to prompt the user to enter a number. The program uses the XOR operator to determine if the number is even or odd. If the operation result is equal to ‘n+1’, the number entered is even, if it isn’t, the number is odd.
Here the number 9 is entered for which the result of the operation is not equal to ‘n+1’. Hence, the number is odd.
Another way to check whether a number is odd or even is by checking the LSB of the number. A binary number’s last digit is its LSB. The LSB of an odd number is always 1, and that of an even number is always 0.
A ternary operator replaces the conventional even odd program in Java using the if-else method to implement the naive logic.
import java.util.Scanner;
public class even_odd {
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
System.out.println("Please enter a number : ");
int n = obj.nextInt();
// java odd or even
String strOutput = (n % 2 == 0) ? "even" : "odd";
System.out.println(n + " is " + strOutput);
obj.close();
}
}
In this case, a string variable named "strOutput" is declared and initialized to odd or even based on whether "n" (integer variable) is divisible by 2. The value of "strOutput" is set using a ternary operator. If the remainder acquired upon dividing ‘n’ by 2 is 0, it is even for which "strOutput" is set to even; if not, it is odd and "strOutput" is set to odd. The result is printed on the console using the “println()” method.
In this instance, we have shown the use of all Bitwise operators, i.e., OR, AND, and XOR, to check whether the input integer variable is even or odd.
//Java Program to Check if Given Integer is Odd or Even
// Using Bitwise OR,AND,XOR
// Importing required classes
import java.util.*;
import java.util.Scanner;
// Main class
public class even_odd {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer variable
// to be checked
//int n = 101;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n= sc.nextInt();
System.out.println("number is: " + n);
// Condition check
// if n|1 if greater than n then this number is even
if ((n | 1) > n) {
// Print statement
System.out.println("Number is Even");
}
else {
// Print statement
System.out.println("Number is Odd");
}
System.out.println("Enter a number");
int x=sc.nextInt();
System.out.println("number is: " + x);
// Condition check
// if x|1 if greater than x then this number is even
if ((x & 1) != 0) {
// Print statement
System.out.println("Number is Odd");
}
else {
// Print statement
System.out.println("Number is Even");
}
System.out.println("Enter a number");
int y=sc.nextInt();
System.out.println("number is: " + y);
// Condition check
// if y|1 if greater than y then this number is even
if ((y ^ 1) == y + 1) {
// Print statement
System.out.println("Number is Even");
}
else {
// Print statement
System.out.println("Number is Odd");
}
}
}
The "even_odd" class imports required classes and defines a public static method, "main", which takes an array of Strings as input argument. Inside the "main" method, an object of the Scanner class is created to take user input. The Scanner object reads an integer input from the user and stores it in the integer variable 'n'.
We first used the Bitwise operator OR to check whether the input number is odd or even. If '(n | 1)' is greater than ‘n,’ then ‘n’ is even. If ‘n’ is odd, the message "Number is Odd" is printed on the console. Here we have used 11 as the input number.
Next, we used the Bitwise AND to check if the integer input stored in the variable ‘x’ is even or odd. If (x&1) equals 0, it is even; if not, it is odd. The number used in this instance is 7, for which the result "Number is Odd" is printed to the console.
Last, the XOR operator checks the integer variable ‘y’ for even or odd. If (y ^ 1) equals (y + 1), then ‘y’ is even. Otherwise, ‘y’ is odd.
For programmers learning the even odd program in Java, using various approaches is essential for a sound base in coding. It is quite simple and easy to understand. If you are new to programming, learning the Even Odd program is equivalent to learning the basics of coding with Java. Master the basics of Java to build a strong career down the line. Consider enrolling in a professional course to understand the language better.
1. How can you create an even odd program in Java using for loop?
We can create two “for” loops to display odd and even numbers. Using the modulus operator in the first loop, check if the remainder is 0. If it is, then the input numbers are even. Next, using the same modulus operator for the second “for” loop, check if the remainder is 1 to verify it is odd.
2. What is the Java program to print odd and even numbers between 1 and 100?
To print odd and even numbers in Java, you can use the even odd program in Java using while loop, the nested if statement method, and for loop.
3. How can you write an even odd program in Java using array?
To print even odd numbers from an array, you have to deduce the remainder of each array element and see if it is divisible by 2. If it is divisible, it is even; if not, it is odd.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...