Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Beginers Problem

  Asked By: Violet    Date: Feb 25    Category: Java    Views: 417
  

i ve just started learning. my question is really of
novie level . you can say i am "hello world" category programmer.
Recently i ve started learning java in my university.
my teacher gave me a assigment in which he said that programm should
judge wether the given number in variable is Even or ODD .The
program should be coded by 4 methods.i ve coded by 2 methods using %
reminder operator and other method is float integer camparsion
method.

the further 2 methods that my teacher and i want is tat
check integer number without using any % and / operators .with only
bitwise operators allowed.
and
3rd last is
check the number by using the conditional operators.

can any one of you help me in this as i am fairly new to programming
and cant find the logic of doing it all ; example codes will be
prefered.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Paul Brooks     Answered On: Feb 25

int x =11
while(x >0){
x-=2;
}
System.out.println(x==0 ? "Even":"Odd");

 
Answer #2    Answered By: Sheryl Morgan     Answered On: Feb 25

Keep in mind that any odd number  can be broken up into an even
number+1. Also remember how numbers are stored in binary (a sum of
different powers of 2). Since an odd  number always has the number 1 in
its sum, you can check the bit for that value by doing a bitwise and.
If the bit is set, then the number's odd.

Conditional:
Do you mean the ternary operator? --> (cond) ? doIfTrue: doIfFalse

The only way I see of using this is (cond)?true:false;, where cond is
any check for an odd number.

 
Didn't find what you were looking for? Find more on Beginers Problem Or get search suggestion and latest updates.




Tagged: