How to Check if a Number Is Odd or Even in Java
- Check if a Number Is Odd or Even in Java
- Use the Ternary Operator to Check if a Number Is Odd or Even in Java
- Use the Bitwise XOR to Check if a Number Is Odd or Even in Java
This article will examine how to use Java to determine whether a number is even or odd. Even numbers are those that are divisible by two, whereas odd numbers are those that are not divisible by two.
This program will determine whether or not the integer is divisible by 2. If the number is divisible, it is an even number; otherwise, it is an odd number.
Check if a Number Is Odd or Even in Java
We’ll explore how to verify whether a number is even or odd when it’s user-defined in this application. This implies that we will first ask the user to input a number, after which we will verify whether the number supplied is even or odd.
import java.util.Scanner;
public class Num {
public static void main(String args[]) {
Scanner oddevn = new Scanner(System.in);
int prdnum;
System.out.println("Provide a number:");
prdnum = oddevn.nextInt();
if (prdnum % 2 == 0)
System.out.println("Provided number is even");
else
System.out.println("Provided number is odd");
}
}
Output:
This program will check whether a number is even or odd. The user provides the number.
An instance of the Scanner
class is created and named oddevn
, which will take user input. Then, a variable prdnum
is declared to store the number initiated by the Scanner
class and inputted by the user.
As we already know, if a number is divisible by 2 and no remainder is left, then the provided number is even. Again, if the number is divided by 2 and a remainder is left, the provided number is odd.
Finally, the outcome is shown, and the program stops working.
Use the Ternary Operator to Check if a Number Is Odd or Even in Java
We’ll examine how to use the ternary operator to determine if an integer is even or odd in this application. This implies that we will first ask the user to input a number and then use the ternary operator to determine if the number supplied is even or odd.
import java.util.Scanner;
public class NewNum {
public static void main(String args[]) {
Scanner oddevn = new Scanner(System.in);
System.out.println("Provide a number: ");
int prdnum = oddevn.nextInt();
String oddEven = (prdnum % 2 == 0) ? "even" : "odd";
System.out.println("Provided number " + prdnum + " is: " + oddEven);
}
}
Output:
This program will work the same as the above program but is slightly different from the previous one as it checks numbers, whether odd or even, using the ternary operator. The ternary operator (?:
) has replaced the if...else
statement in the above program.
If the integer is divisible by two, the result is even
; otherwise, the value odd
is returned. The result is recorded in the oddEven
string variable.
After that, string concatenation is used to print the output on the screen.
Use the Bitwise XOR to Check if a Number Is Odd or Even in Java
We’ll explore how to use the bitwise XOR to determine if an integer is even or odd in this application. The idea behind this method is that bitwise XORing an even number by 1 will increase the value of the number by 1, whereas bitwise XORing an odd number by 1 will decline the value by 1.
import java.util.Scanner;
public class NewNumNum {
public static void main(String args[]) {
Scanner oddevn = new Scanner(System.in);
System.out.println("Provide a number: ");
int prdnum = oddevn.nextInt();
if ((prdnum ^ 1) == prdnum + 1) {
System.out.println("The provided number " + prdnum + " is Even");
} else {
System.out.println("The provided number " + prdnum + " is Odd");
}
}
}
Output:
Shiv is a self-driven and passionate Machine learning Learner who is innovative in application design, development, testing, and deployment and provides program requirements into sustainable advanced technical solutions through JavaScript, Python, and other programs for continuous improvement of AI technologies.
LinkedIn