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
C program to find all possible outcomes of a dice?
what is the difference between class and unio?
What is modeling?
Why do we use c for the speed of light?
Write a function which takes as parameters one regular expression(only ? and * are the special characters) and a string and returns whether the string matched the regular expression.
What is sizeof return in c?
how can i access hard disk address(physical address)? are we access hard disk by using far,near or huge pointer? if yes then please explain.....
Explain enumerated types in c language?
Is a pointer a kind of array?
Write a program to swap two numbers without using third variable in c?
I need a help with a program: Write a C program that uses data input in determining the whole of points A and a whole of circles B. Find two points in A so that the line which passes through them, cut through the maximum number of circles.
How is a null pointer different from a dangling pointer?
What are the types of macro formats?
In c programming write a program that will print 10 multiples of 3 except 15,18,21 using looping
What is context in c?