Q:
How to identify a given positive decimal number as even/odd without using % or / operator ?
Answer
You may be very good at coding,but if you questioning how on earth you could solve this problem.Then here is a solution. If you remember the good old days of our primary school then the solution is easy,"division is a matter of iterative subtraction".
public class TestEvenOdd {
public static void main(String arg[ ]){
int num=6;
int result=num;
while(result>=2){
result=result-2;
}
if(result==1){
System.out.println("The number is odd");
}else{
System.out.print("The number is even");
}
}
}
View answer
Workspace
Report Error
Discuss