# a problem

Discussion in 'Java' started by bharath539@gmail.com, Feb 10, 2007.

1. ### Guest

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

, Feb 10, 2007

2. ### Gordon BeatonGuest

On 10 Feb 2007 06:15:46 -0800, wrote:
> 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

Yes, there is. Problem solved!

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e

Gordon Beaton, Feb 10, 2007

3. ### Andrew ThompsonGuest

On Feb 11, 1:15 am, wrote:
> hi,
> any one of the group ,please solve my ..

...homework? What good would that do you?

Andrew T.

Andrew Thompson, Feb 10, 2007
4. ### bharathGuest

On Feb 10, 7:15 pm, wrote:
> 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

bharath, Feb 10, 2007
5. ### bharathGuest

On Feb 10, 7:18 pm, Gordon Beaton <> wrote:
> On 10 Feb 2007 06:15:46 -0800, wrote:
>
> > 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

>
> Yes, there is. Problem solved!
>
> /gordon
>
> --
> [ don't email me support questions or followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e

then post me the solution.Mr.Beaton

bharath, Feb 10, 2007
6. ### Andrew ThompsonGuest

On Feb 11, 3:32 am, "bharath" <> wrote:
> On Feb 10, 7:18 pm, Gordon Beaton <> wrote:
>
> > On 10 Feb 2007 06:15:46 -0800, wrote:

>
> > > 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!

...
> 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
or at least to consider what it means,

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

Andrew T.

Andrew Thompson, Feb 10, 2007
7. ### Gordon BeatonGuest

On 10 Feb 2007 08:32:22 -0800, bharath wrote:
> then post me the solution.Mr.Beaton

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

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e

Gordon Beaton, Feb 10, 2007
8. ### Flo 'Irian' SchaetzGuest

And thus spoke ...

> 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

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

Flo

Flo 'Irian' Schaetz, Feb 10, 2007
9. ### JeffGuest

On Feb 10, 11:30 am, "bharath" <> wrote:
> On Feb 10, 7:15 pm, wrote:
>
> > 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

>
> 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.

Jeff, Feb 10, 2007
10. ### Chris UppalGuest

Jeff wrote:

> 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

Chris Uppal, Feb 11, 2007
11. ### JohnGuest

Jeff wrote:

>
>
> 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...

John, Feb 11, 2007
12. ### LewGuest

bharath wrote:
> Mr.Beaton,please send me the solution.

I'm afraid it doesn't work that way.

- Lew

Lew, Feb 11, 2007
13. ### Gordon BeatonGuest

On Sun, 11 Feb 2007 02:59:52 GMT, John wrote:
> 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

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e

Gordon Beaton, Feb 11, 2007
14. ### Faton BerishaGuest

On Feb 10, 3:15 pm, wrote:
> 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

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;

Faton Berisha

Faton Berisha, Feb 11, 2007
15. ### Andrew ThompsonGuest

On Feb 11, 9:20 pm, "Faton Berisha" <> wrote:
> On Feb 10, 3:15 pm, wrote:
>
> > 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

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

Why should the OP do that?
- get people like you to do it.
- get a job as your boss.

Andrew T.

Andrew Thompson, Feb 11, 2007
16. ### Guest

On Feb 10, 7:15 pm, wrote:
> 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

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

, Feb 11, 2007
17. ### JohnGuest

wrote:
> 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
>

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 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

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.

John, Feb 11, 2007
18. ### Chris UppalGuest

[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]

John wrote:

> 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

Chris Uppal, Feb 11, 2007
19. ### LewGuest

wrote:
>>> 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

"Faton Berisha" wrote:
>> First of all, you should really solve your own homework;
>> this is it's purpose.

Andrew Thompson wrote:
> Why should the OP do that?
> - 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

Lew, Feb 11, 2007
20. ### LewGuest

wrote:
>> 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

wrote:
> 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

Lew, Feb 11, 2007