# Find a small and large number from 3 numbers

Page 1 of 1

## 10 Replies - 15578 Views - Last Post: 22 December 2008 - 08:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=77829&amp;s=ecad65ee553eafdb55913d4cbda746c7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 wawa

Reputation: 0
• Posts: 16
• Joined: 22-December 08

# Find a small and large number from 3 numbers

Posted 22 December 2008 - 03:06 AM

Hi.. is anyone can help me to solve this prob..

The Question: To compare between 3 variables which is x=50, y=350 and z=250 and find the largest value and the smallest value between this 3 variables.

<?php

\$x = 50;
\$y = 350;
\$z = 250;

echo "x value is \$x<br>";
echo "y value is \$y<br>";
echo "z value is \$z<br>";

if (\$x > \$y || \$x >\$z)
{
echo "Value x is \$x";
echo "Value x is large than value y and z";
}

else if (\$x < \$y || \$x < \$z)
{
echo "Value x is \$x";
echo "Value x is small than value y and z";
}

else if (\$y > \$x || \$y >\$z)
{
echo "Value y is \$y";
echo "Value y is large than value x and z";
}
else if (\$y < \$x || \$y < \$z)
{
echo "Value y is \$x";
echo "Value y is small than value x and z";
}
else if (\$z > \$x || \$z < \$y)
{
echo "Value z is \$z";
echo "Value z is small than value x and y";
}
else if (\$z < \$x || \$z < \$y)
{
echo "Value z is \$z";
echo "Value z is small than value x and y";
}

?>

i want to find a small and large number from this 3 number, but the output from this coding only the small number appear.... i want large and small number appear..is there anyone can help me or correct my prob and show whre wrong..?

Is This A Good Question/Topic? 0

## Replies To: Find a small and large number from 3 numbers

### #2 kummu4help

Reputation: 4
• Posts: 245
• Joined: 05-August 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 03:35 AM

try this
```<?php
\$x=30;
\$y=40;
\$z=60;

\$maxValue=max(\$x,max(\$y,\$z));
\$minValue=min(\$x,min(\$y,\$z));

print \$maxValue;
print \$minvalue;
?>

```

### #3 wawa

Reputation: 0
• Posts: 16
• Joined: 22-December 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 06:29 AM

kummu4help, on 22 Dec, 2008 - 02:35 AM, said:

try this
```<?php
\$x=30;
\$y=40;
\$z=60;

\$maxValue=max(\$x,max(\$y,\$z));
\$minValue=min(\$x,min(\$y,\$z));

print \$maxValue;
print \$minvalue;
?>

```

Thanks kummu4help for ur help but in need to solve this prob using if..if else..else control structure..any idea?

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3778
• Posts: 13,688
• Joined: 08-August 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 08:03 AM

```If(\$x>\$y){
\$max=\$x;
\$min=\$y;
} else
{
\$max=\$y;
\$min=\$x;
}
if(\$max<\$z){
\$max=\$z;
}
if{\$min>\$z){
\$min=\$z;
}
```

### #5 ~NJ

Reputation: 9
• Posts: 52
• Joined: 22-December 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 05:29 PM

You might be better off placing the values in an array and using some array specific functions to calculate the highest and lowest values. IE: sort();

I really don't know why you need it done using if..else statements. For one, it doesn't make your code very readable and you're typing a whole lot more than you need to. Not to mention the server performance...

### #6 wawa

Reputation: 0
• Posts: 16
• Joined: 22-December 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 05:42 PM

~NJ, on 22 Dec, 2008 - 04:29 PM, said:

You might be better off placing the values in an array and using some array specific functions to calculate the highest and lowest values. IE: sort();

I really don't know why you need it done using if..else statements. For one, it doesn't make your code very readable and you're typing a whole lot more than you need to. Not to mention the server performance...

owh...i will try by using sort n thanks for helping..

### #7 CTphpnwb

• D.I.C Lover

Reputation: 3778
• Posts: 13,688
• Joined: 08-August 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 05:43 PM

I'm guessing that it's for a class.

### #8 ~NJ

Reputation: 9
• Posts: 52
• Joined: 22-December 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 05:48 PM

It's quite simple. All you'd need to do is something like:
```\$numbers = array('50', '350', '250');
```

Then simply sort them. The following function sorts them from lowest to highest:
```sort(\$numbers);
```

Or (in your case) the following function should be used to sort the values from highest to lowest:
```rsort(\$numbers);
```

Then you could simply echo out the highest, middle and lowest values:
```echo "Highest value is: ";
echo \$numbers[0];
echo "The middle number is: ";
echo \$numbers[1];
echo "The lowest value is: ";
echo \$numbers[2];

```

### #9 wawa

Reputation: 0
• Posts: 16
• Joined: 22-December 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 06:49 PM

~NJ, on 22 Dec, 2008 - 04:48 PM, said:

It's quite simple. All you'd need to do is something like:
```\$numbers = array('50', '350', '250');
```

Then simply sort them. The following function sorts them from lowest to highest:
```sort(\$numbers);
```

Or (in your case) the following function should be used to sort the values from highest to lowest:
```rsort(\$numbers);
```

Then you could simply echo out the highest, middle and lowest values:
```echo "Highest value is: ";
echo \$numbers[0];
echo "The middle number is: ";
echo \$numbers[1];
echo "The lowest value is: ";
echo \$numbers[2];

```

i got it..thank a lot..

#### Attached File(s)

•  A2S1.php (435bytes)
•  A2S1.php (435bytes)

### #10 wawa

Reputation: 0
• Posts: 16
• Joined: 22-December 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 07:21 PM

wawa, on 22 Dec, 2008 - 05:49 PM, said:

~NJ, on 22 Dec, 2008 - 04:48 PM, said:

It's quite simple. All you'd need to do is something like:
```\$numbers = array('50', '350', '250');
```

Then simply sort them. The following function sorts them from lowest to highest:
```sort(\$numbers);
```

Or (in your case) the following function should be used to sort the values from highest to lowest:
```rsort(\$numbers);
```

Then you could simply echo out the highest, middle and lowest values:
```echo "Highest value is: ";
echo \$numbers[0];
echo "The middle number is: ";
echo \$numbers[1];
echo "The lowest value is: ";
echo \$numbers[2];

```

i got it..thank a lot..

<?php

echo "Before Sorting <br>";
\$numbers = array(50, 350, 250);
echo \$numbers[0]. "<br>" ;
echo \$numbers[1]. "<br>" ;
echo \$numbers[2]. "<br>" ;

echo "After Sorting <br>";
\$numbers = array(50, 350, 250);
rsort(\$numbers);
for (\$i=0; \$i <= 2; \$i++)
echo \$numbers[\$i]."<br \>";

echo "Highest value is: <br>";
echo \$numbers[0]. "<br>" ;
echo "The lowest value is: <br>";
echo \$numbers[2]

?>

### #11 ~NJ

Reputation: 9
• Posts: 52
• Joined: 22-December 08

## Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 08:35 PM