a problem

B

bharath539

hi,
any one of the group ,please solve my problem
"is there is any method to find given number is even
or odd without using if ,else ,for,while,switch,conditional
operators,if else ladders...."
 
G

Gordon Beaton

any one of the group ,please solve my problem
"is there is any method to find given number is even
or odd without using if ,else ,for,while,switch,conditional
operators,if else ladders...."

Yes, there is. Problem solved!

/gordon
 
B

bharath

hi,
any one of the group ,please solve my problem
"is there is any method to find given number is even
or odd without using if ,else ,for,while,switch,conditional
operators,if else ladders...."

Mr.Beaton,please send me the solution.
 
A

Andrew Thompson

...
then post me the solution.Mr.Beaton

(chuckle) More effective than making
demands of G.B. (or just about any
other member of these groups), might
be to answer the question I asked you,
or at least to consider what it means,
and reply to it.

After all, we are having a discussion,
are we not?

Andrew T.
 
G

Gordon Beaton

then post me the solution.Mr.Beaton

I already answered the question you asked.

However if you want to know *how* to determine the parity of a number
without using the mechanisms you mentioned earlier, then I'll give you
a hint: how would *you* (not a computer) decide whether a given number
was even or odd?

/gordon
 
F

Flo 'Irian' Schaetz

And thus spoke (e-mail address removed)...
any one of the group ,please solve my problem
"is there is any method to find given number is even
or odd without using if ,else ,for,while,switch,conditional
operators,if else ladders...."

"==" is allowed? Then it's easy to do.

Flo
 
J

Jeff

Mr.Beaton,please send me the solution.


I would use a bitwise operator. But that's just me. It's not my
assignment, and I always hate assignments that artificially limit
choices - if teaching Java, why give an assignment that makes it so
you can't use normal Java constructs? I teach in a medical school and
this would be like asking how a student would diagnose pneumonia
without xray or stethoscope, and they can only ask two questions...
Useless.
 
C

Chris Uppal

Jeff said:
I would use a bitwise operator. But that's just me. It's not my
assignment, and I always hate assignments that artificially limit
choices - if teaching Java, why give an assignment that makes it so
you can't use normal Java constructs?

I suspect that a large part of this particular exercise is about understanding
boolean-valued expressions, as something more general than the XXX that goes in
an if (XXX) test.

-- chris
 
J

John

Jeff said:
I would use a bitwise operator. But that's just me. It's not my
assignment, and I always hate assignments that artificially limit
choices - if teaching Java, why give an assignment that makes it so
you can't use normal Java constructs? I teach in a medical school and
this would be like asking how a student would diagnose pneumonia
without xray or stethoscope, and they can only ask two questions...
Useless.

If you were to use the result of modulus as an index to an array of
"state".... well that's what I tried... it ain't pretty...
 
G

Gordon Beaton

If you were to use the result of modulus as an index to an array of
"state".... well that's what I tried... it ain't pretty...

The result of the modulus only needs to be compared with one of two
possible values, and it's about as straightforward as it gets.

/gordon
 
F

Faton Berisha

hi,
any one of the group ,please solve my problem
"is there is any method to find given number is even
or odd without using if ,else ,for,while,switch,conditional
operators,if else ladders...."

First of all, you should really solve your own homework;
this is it's purpose.

However, if the == operator is allowed, then the solution is simple

System.out.println(n % 2 == 0);

or, a fancier one,

System.out.println((n & 1) == 0);

In the other case, i.e., if == is not allowed,
it becomes more cumbersome:

int n = 15;
boolean[] answer = {true,false};
System.out.println(answer[n % 2]);
// System.out.println(answer[n & 1]);

Faton Berisha
 
A

Andrew Thompson

First of all, you should really solve your own homework;
this is it's purpose.

Why should the OP do that?
When they can instead..
- get people like you to do it.
- get a job at your firm and steal your code.
- get a job as your boss.

Andrew T.
 
S

susauparna

hi,
any one of the group ,please solve my problem
"is there is any method to find given number is even
or odd without using if ,else ,for,while,switch,conditional
operators,if else ladders...."

a number is odd if it is not divisible by 2 or the last bit of the
number is not set .
so just do this ..
number & 1 i.e operate bit wise and with number and 1 if its 0 then
number is even else odd
 
J

John

hi,
any one of the group ,please solve my problem
"is there is any method to find given number is even
or odd without using if ,else ,for,while,switch,conditional
operators,if else ladders...."
As you can probably tell, there are a multitude of ways to address your
problem. If you walk slowly through the various responses to your
initial posting, I am sure you will find something that will help you.
However, I think that most people will be willing to help if you can
show that you have done some initial research on your own. Tell us what
you have tried, show us code and output. I'm not exactly experience in
Java, so when you asked your question, I simply did a google searh on
"java method even odd" and looked at what google told me.

Personally, I don't see why your professor would restrict you in how you
can do your code, unless he/she was trying to get you to "think outside
the box". Unfortunantly, we don't know what your level of knowledge is,
so if we were to prescribe a solution that makes use of bitwise
operators (beyond me right now) and you present it as your solution,
your professor might penalize you thinking that you have cheated in some
way. So perhaps you can tell us briefly what you know about in Java so
that if we can help you, we won't get you in trouble :)

However, if you are so lazy that you want someone else to do your
homework for you, then you are asking in the wrong forum. People here
do not take kindly to posters who ask for them to do their homework
assignments, it kind of defeats the purpose of learning on your own.

Finally, there is a wonderful web page/usenet posting

http://groups.google.ca/group/comp....154f9/f3a348bfaba195c6?hl=en#f3a348bfaba195c6

I would suggest that you read this, adopt it at your creedo, memorize
it, sleep with it under your pillow, what ever it takes to make it your
modus operendi when posting. It will make your experience here much
more enjoyable.
 
C

Chris Uppal

[I sent this yesterday, but it doesn't seem to be showing up on my server.
I'll try one more time. Apologies to anyone who sees it twice -- doubly so to
John if the reason is that I emailed it to him instead of posting it]
Personally, I don't see why your professor would restrict you in how you
can do your code, unless he/she was trying to get you to "think outside
the box".

I suspect that a large part of this particular exercise is about understanding
boolean-valued expressions, as something more general than the XXX that goes in
an if (XXX) test.

-- chris
 
L

Lew

Andrew said:
Why should the OP do that?
When they can instead..
- get people like you to do it.
- get a job at your firm and steal your code.
- get a job as your boss.

I think someone like the OP has done that last to me a number of times.

- Lew
 
L

Lew

a number is odd if it is not divisible by 2 or the last bit of the
number is not set .
so just do this ..
number & 1 i.e operate bit wise and with number and 1 if its 0 then
number is even else odd

And someone who can't figure that out without your help needs to change their
major to toenail clipping.

Why are we spoon-feeding this homework solution to them?

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top