10 Replies - 7304 Views - Last Post: 22 December 2008 - 08:35 PM Rate Topic: -----

#1 wawa  Icon User is offline

  • New D.I.C Head

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

Find a small and large number from 3 numbers

Post icon  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.

My answer:
<?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..? :rolleyes:

Is This A Good Question/Topic? 0
  • +

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

#2 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • 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;
?>


Was This Post Helpful? 0
  • +
  • -

#3 wawa  Icon User is offline

  • New D.I.C Head

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

Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 06:29 AM

View Postkummu4help, 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? :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,885
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#5 ~NJ  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#6 wawa  Icon User is offline

  • New D.I.C Head

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

Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 05:42 PM

View Post~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..
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,885
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 ~NJ  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • 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];


Was This Post Helpful? 0
  • +
  • -

#9 wawa  Icon User is offline

  • New D.I.C Head

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

Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 06:49 PM

View Post~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)

  • Attached File  A2S1.php (435bytes)
    Number of downloads: 36
  • Attached File  A2S1.php (435bytes)
    Number of downloads: 37

Was This Post Helpful? 0
  • +
  • -

#10 wawa  Icon User is offline

  • New D.I.C Head

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

Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 07:21 PM

View Postwawa, on 22 Dec, 2008 - 05:49 PM, said:

View Post~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]

?>

Was This Post Helpful? 0
  • +
  • -

#11 ~NJ  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 22-December 08

Re: Find a small and large number from 3 numbers

Posted 22 December 2008 - 08:35 PM

No problem. I'm glad I could help you out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1