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

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

1. ## 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. ## 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?

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

5. ## Re: Checking if number is in between two other numbers?

hint: compute the lower/upper bounds

6. ## 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. ## 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. ## Re: Checking if number is in between two other numbers?

`System.out.println(b > (a < c ? a : c) && b < (a > c ? a : c));`

9. ## Re: Checking if number is in between two other numbers?

Originally Posted by Junky
`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. ## 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.