# Checking if number is in between two other numbers?

• November 11th, 2011, 09:35 PM
colerelm
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?
• November 11th, 2011, 09:37 PM
KevinWorkman
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?
• November 11th, 2011, 09:58 PM
colerelm
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...
• November 11th, 2011, 10:02 PM
KevinWorkman
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.
• November 11th, 2011, 10:30 PM
helloworld922
Re: Checking if number is in between two other numbers?
hint: compute the lower/upper bounds
• November 16th, 2011, 01:19 PM
DougFane
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.
• November 17th, 2011, 06:12 AM
Mr.777
Re: Checking if number is in between two other numbers?
Let's say;
Code :

```int a=5; int b=4;```
What will
Code :

`int c=a/b;`
results?
• November 17th, 2011, 10:54 PM
Junky
Re: Checking if number is in between two other numbers?
Code java:

`System.out.println(b > (a < c ? a : c) && b < (a > c ? a : c));`
• November 17th, 2011, 11:30 PM
Mr.777
Re: Checking if number is in between two other numbers?
Quote:

Originally Posted by Junky
Code java:

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

I think OP means without using < or > operators. Did i misunderstand?
• November 17th, 2011, 11:34 PM
Junky
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.