To find whether a number is even or odd without using any
conditional operator??
Answer Posted / yogeshkumar
Check given number is even or odd without using modulo operator.
for this we use & operator.
if any number is odd it must have right most bit 1.
example:
int i=5;
binary form i= 0101
now use & operator
int j=i&1;[0101&1]//
here j have 0001;
public class EvenandOddNumber {
public static void main(String[] args) {
int i=5;
int j=i&1;
if(j>0){
System.out.println("odd");
}
else {
System.out.println("even");
}
}
}
| Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
Is there any demerits of using pointer?
What is echo in c programming?
exit () is used to a) exit () terminates the execution of the program itself b) exit () terminates the execution of the loop c) exit () terminates the execution of the block d) none of the above
What are the different types of control structures in programming?
How can I write a function that takes a format string and a variable number of arguments?
What is omp_num_threads?
What is the use of parallelize in spark?
Define Array of pointers.
What are the uses of a pointer?
How can a process change an environment variable in its caller?
how to find anagram without using string functions using only loops in c programming
Which is best linux os?
What is a structure member in c?
Difference between exit() and _exit() function?
Is fortran faster than c?