PHP Program to find greatest of three number


October 27, 2023, Learn eTutorial
4698

How to find the greatest among the three numbers?

To find the greatest of three numbers, we can follow many ways, In this PHP program, after getting three numbers from the user. Check if the first number is greater than the second and third numbers, if it is true, print the first number as the greatest. If this test fails, then compare the second and third numbers and print the greatest.

For example, if the entered numbers are 5 8 9 then the output will be 9.

In PHP, there is a predefined function for checking the maximum value.max() function returns the highest value from the array or list of elements.

It is the easiest way of finding the greatest number.


<?php
  $num1 = readline("Enter the 1st number: ");
  $num2 = readline("Enter the 2nd number: ");
  $num3 = readline("Enter the 3rd number: ");
  echo "Greatest among $num1 $num2 $num3 is " . max($num1, $num2, $num3);
 ?>
 

How to find the greatest among the three numbers using user defined function in PHP?

cfind greatest of three number

In this PHP program, we are using the user-defined function checkNum() to find the greatest among the three numbers.

  • First, we have to accept the three values into the variable num1, num2, num3 and pass these values as the argument of the user-defined function checkNum().
  • Print the return value of the function as the greatest of the three numbers.

Inside the Function

In the function checkNum() we will be getting three parameters n1, n2, n3, Now we have to check the conditions 'n1 > n2' and 'n1 > n3', then assign the value of the variable n1 to the variable greatest.

Otherwise, check the condition 'n2 > n3' if true assign the value of the variable n2 to the variable greatest.

Else assign the value of the variable n3 to the variable greatest. Finally, return the variable greatest as the greatest of the three numbers.

ALGORITHM

STEP 1: Accept the three values from the user into the variables num1, num2, num3

STEP 2: Print the return value of the user-defined function checkNum() with arguments num1, num2, num3 as the greatest among the three numbers entered.

function: checkNum(n1, n2, n3)

STEP 1: Check the condition 'n1 > n2' and 'n1 > n3' if both are true assign the value of the variable n1 to the variable greatest otherwise go to step 2

STEP 2: Check the condition 'n2 > n3' if the condition is true assign the value of the variable n2 to the variable greatest otherwise go to step 3

STEP 3: Assign the value of the variable n3 to the variable greatest

STEP 4: Return the value of the variable greatest as the greatest of the three numbers

PHP Source Code

                                          <?php
function checkNum($n1, $n2, $n3)
{
    if ($n1 > $n2 && $n1 > $n3)
        $greatest= $n1;
    elseif ($n2 > $n3)
        $greatest= $n2;
    else
        $greatest= $n3;
    return $greatest;
}
$num1 = readline("Enter the 1st number: ");
$num2 = readline("Enter the 2nd number: ");
$num3 = readline("Enter the 3rd number: ");
echo "Greatest among $num1 $num2 $num3 is " . checkNum($num1, $num2, $num3);
?>
                                      

OUTPUT

Enter the 1st number: 7
Enter the 2nd number: 45
Enter the 3rd number: 20
Greatest among 7 45 20 is 45