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
write a program in C that prompts the user for today's date,tomorrow's date and display the results.Use structures for today's date,tomorrow's date and an array to hold the days for each month of the year.
How can a process change an environment variable in its caller?
How can I avoid the abort, retry, fail messages?
Write a program to implement a round robin scheduler and calculate the average waiting time.Arrival time, burst time, time quantum, and no. of processes should be the inputs.
Tell us bitwise shift operators?
What is extern storage class in c?
What are formal parameters?
What are the different types of C instructions?
How can I ensure that integer arithmetic doesnt overflow?
What is the use of clrscr?
What is keyword with example?
Is there a way to jump out of a function or functions?
Can you mix old-style and new-style function syntax?
Why enum is used in c?
Is sizeof a keyword in c?