How to find whether a number is odd or even, without using if
condition or ternary operators in Java?
This question is given by my teacher. He also give me a hint that it is possible by using a bitwise operator.
How to find whether a number is odd or even, without using if
condition or ternary operators in Java?
This question is given by my teacher. He also give me a hint that it is possible by using a bitwise operator.
There are few ways to not use if
and get behavior that will be same as if if
was used, like ternary operator condition ? valueIfTrue : valueIfFalse
or switch/case
.
But to be tricky you can also use arrays and try to figure some transformation of our value to proper array index. In this case your code could look like
int number = 13;
String[] trick = { "even", "odd" };
System.out.println(number + " is " + trick[number % 2]);
output:
13 is odd
You can change number % 2
with number & 1
to use suggestion of your teacher. Explanation of how it works can be found here.
Consider a number's representation in binary format (E.g., 5 would be 0b101). An odd number has a "1" as its singles digit, an even number had a zero there. So all you have to do is bitwise-and it with 1 to extract only that digit, and examine the result:
public static boolean isEven (int num) {
return (num & 1) == 0;
}
int isOdd = (number & 1);
isOdd
will be 1
if number is odd, otherwise it will be 0
.
Did you mean something like this?
boolean isEven(int value) {
return value % 2 == 0;
}
boolean isOdd(int value) {
return value % 2 == 1;
}
Every odd number have 1 at the end of its binary representation.
Sample :
public static boolean isEven(int num) {
return (num & 1) == 0;
}
Just saw now 'Without using IF'
boolean isEven(double num) { return (num % 2 == 0) }
Just a quick wrapper over the already defined process...
public String OddEven(int n){
String oe[] = new String[]{"even","odd"};
return oe[n & 1];
}
I would use:
( (x%2)==0 ? return "x is even" : return "x is odd");
One line code.
# /* **this program find number is odd or even without using if-else,
## switch-case, neither any java library function...*/
##//find odd and even number without using any condition
class OddEven {
public static void main(String[] args) {
int number = 14;
String[] trick = { "even", "odd" };
System.out.println(number + " is " + trick[number % 2]);
}
}
/**************OUTPUT*************
// 14 is even
// ...................
//13 is odd
Method 1:
System.out.println(new String[]{"even","odd"}[Math.abs(n%2)]);
Method 2:
System.out.println(new String[]{"odd","even"}[(n|1)-n]);
Method 1 differs from the accepted answer in the way that it accounts for negative numbers as well, which are also considered for even/odd.
import java.util.Scanner;
public class EvenOddExample
{
public static void main(String[] args)
{
System.out.println("\nEnter any Number To check Even or Odd");
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
int no1=no;
while (no>1)
{
no=no-2;
}
if(no==0)
{
System.out.println(no1 +" is evenNumber");
}
else
{
System.out.println(no1 +" is odd Number");
}
}
}
you can also use bitwise shift operators (number >> 1)<<1 == number then even else odd
Well, yes you can make use of bitwise operator. Here's an example,
public class EvenOddBitwise
{
public static void main(String[] args)
{
int number = 64;
if((number & 1) == 1)
{
System.out.println(number + " is Odd.");
}
else
{
System.out.println(number + " is Even.");
}
}
}