# PHP Program to find the smallest of the three number

May 10, 2022, Learn eTutorial
1831

## How to find the smallest among the three numbers?

In this program, we need to check which among the three numbers entered is the smallest, and we can do that by checking and comparing each number with the other and taking the smallest. Again compare the smallest with the last number to get the final result. For example, if the entered numbers are 5 8 9 then the output will be 5.

## How to find the smallest among the three numbers using PHP?

In this program, we are using the user-defined function checkNum() to find the smallest 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() and print the return of the function as the smallest of the three numbers. In the function checkNum() we will be getting three parameters n1, n2, n3, and first, we have to check the conditions 'n1 < n2' and 'n1 < n3' if both the conditions are true assign the value of the variable n1 to the variable temp otherwise check the condition 'n2 < n3' if true assign the value of the variable n2 to the variable temp otherwise assign the value of the variable n3 to the variable temp and after all the return the value of the variable temp as the smallest 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 user-defined function checkNum() with arguments num1, num2, num3 is the smallest among the three numbers entered

### ALGORITHM 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 temp 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 temp otherwise go to step 3

Step 3: Assign the value of the variable n3 to the variable temp

Step 4: Return the value of the variable temp as the smallest of the three numbers

## PHP Source Code

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

## OUTPUT

```Enter the 1st number: 567
Enter the 2nd number: 123
Enter the 3rd number: 256
Smallest among 567 123 256 is 123```

VIEW ALL
VIEW ALL