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 infinite loop?
how do you execute a c program in unix.
What are the types of operators in c?
What is a nested formula?
How do c compilers work?
What is modifier & how many types of modifiers available in c?
Write a program of advanced Fibonacci series.
What is the use of clrscr?
Explain how do you print only part of a string?
Why are algorithms important in c program?
Can you please explain the difference between strcpy() and memcpy() function?
Why isn't any of this standardized in c? Any real program has to do some of these things.
What are the 4 types of programming language?
a formula,a series of steps,or well defined set of rules for solving a problem a) algorithem b) program c) erdiagram d) compiler
Is there any data type in c with variable size?