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
What is wrong with this declaration?
What is the scope of static variable in c?
Is linux written in c?
How will you write a code for accessing the length of an array without assigning it to another variable?
Why clrscr is used after variable declaration?
What does void main return?
Linked lists -- can you tell me how to check whether a linked list is circular?
What are the types of variables in c?
Why is a semicolon (;) put at the end of every program statement?
How many types of operator or there in c?
What are valid operations on pointers?
What are global variables and explain how do you declare them?
What is the difference between functions getch() and getche()?
What is the difference between declaring a variable and defining a variable?
What is New modifiers?