Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Checking if number is in between two other numbers?

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Checking if number is in between two other numbers?

    Say I have three numbers A, B and C. How would I check if B lies between A and C without assuming that A < C or A > C?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Checking if number is in between two other numbers?

    How would you do this without a computer? How do you do it in your head?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checking if number is in between two other numbers?

    lol that's what im trying to figure out....What I've thought of so far was thinking of it as a distance or absolute value....Or maybe even finding the median between both numbers and going from there. But I'm still not sure what I should do...

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Checking if number is in between two other numbers?

    A big part of figuring out an algorithm is taking things that we do in our heads without really thinking about and breaking them up into smaller individual steps. Pretend you have a really dumb friend who has no idea how to figure out whether a number is in between two other numbers. Write out directions for him that he could follow to accomplish the task. When you have that written out, you should have an algorithm that should be pretty easily translated into code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Checking if number is in between two other numbers?

    hint: compute the lower/upper bounds

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checking if number is in between two other numbers?

    hint: you said you can't assume that a < c or a > c, and you're correct. But you CAN figure it out, and then work form there.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Checking if number is in between two other numbers?

    Let's say;
    int a=5;
    int b=4;
    What will
    int c=a/b;
    results?

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Checking if number is in between two other numbers?

    System.out.println(b > (a < c ? a : c) && b < (a > c ? a : c));
    Improving the world one idiot at a time!

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Checking if number is in between two other numbers?

    Quote Originally Posted by Junky View Post
    System.out.println(b > (a < c ? a : c) && b < (a > c ? a : c));
    I think OP means without using < or > operators. Did i misunderstand?

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Checking if number is in between two other numbers?

    I took it to mean that A could be less than C or A could be greater than C. Either could be true so you would have to test both cases.
    Improving the world one idiot at a time!

Similar Threads

  1. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  2. Checking Blurbs
    By 9erNumber16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2011, 07:29 PM
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  4. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM
  5. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM