Sunday, September 4, 2016

How to identify a given positive decimal number as even/odd without using % or / operator ?

A plain trap, even for those experienced people. 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");
}
}
}


Bhanu Namikaze

Author & Editor

Bhanu Namikaze is an Ethical Hacker, Passionate Blogger, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors, Surfing and Social Networking. Feel Free To Contact Me @ Bhanu.

0 comments:

Post a Comment

Popular Posts

Copyrights @ Interview Questions.Me - Blogger Templates Designed by Templateism - Distributed By Protemplateslab

  • (91) 5544 654942
  • Bhanu@HackingDream.net
  • Interview Questions

Copyrights @ Interview Questions.Me -A Site By Bhanu In Association With Hacking Dream