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
    operators,if else ladders...."
     
    , Feb 10, 2007
    #1
    1. Advertising

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

    /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
    #2
    1. Advertising

  3. 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
    #3
  4. bharath 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
    > operators,if else ladders...."


    Mr.Beaton,please send me the solution.
     
    bharath, Feb 10, 2007
    #4
  5. bharath Guest

    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!
    >
    > /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
    #5
  6. 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
    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.
     
    Andrew Thompson, Feb 10, 2007
    #6
  7. On 10 Feb 2007 08:32:22 -0800, bharath wrote:
    > 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

    --
    [ 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
    #7
  8. 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
    > operators,if else ladders...."


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

    Flo
     
    Flo 'Irian' Schaetz, Feb 10, 2007
    #8
  9. Jeff Guest

    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
    > > operators,if else ladders...."

    >
    > 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
    #9
  10. Chris Uppal Guest

    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
    #10
  11. John Guest

    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
    #11
  12. Lew Guest

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


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

    - Lew
     
    Lew, Feb 11, 2007
    #12
  13. 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
    #13
  14. 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
    > 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
     
    Faton Berisha, Feb 11, 2007
    #14
  15. 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
    > > operators,if else ladders...."

    >
    > 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.
     
    Andrew Thompson, Feb 11, 2007
    #15
  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
    > 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
     
    , Feb 11, 2007
    #16
  17. John Guest

    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
    > 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.
     
    John, Feb 11, 2007
    #17
  18. Chris Uppal Guest

    [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
    #18
  19. Lew Guest

    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
    >>> operators,if else ladders...."


    "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?
    > 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
     
    Lew, Feb 11, 2007
    #19
  20. Lew Guest

    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
    >> operators,if else ladders...."


    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
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,031
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,220
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    2,001
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    421
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    581
    Bullschmidt
    May 11, 2004
Loading...

Share This Page