Find largest and smallest number using php

GOCOURSE

Find largest and smallest number from 100,205,345,465,565,696,777,869,999

Program: 

<?php
$numbers = array(100,205,345,465,565,696,777,869,999);
// Find the largest number
$largest = $numbers[0];
foreach ($numbers as $num) {
  if ($num > $largest) {
    $largest = $num;
  }
}
// Find the smallest number
$smallest = $numbers[0];
foreach ($numbers as $num) {
  if ($num < $smallest) {
    $smallest = $num;
  }
}
echo "The largest number is: " . $largest . "\n";
echo "The smallest number is: " . $smallest;
?>

Output:

The largest number is: 999
The smallest number is: 100

Find largest and smallest number from 1,2,3,4,5,6,7,8,9

Program:

<?php
$numbers = array(1,2,3,4,5,6,7,8,9);
// Find the largest number
$largest = $numbers[0];
foreach ($numbers as $num) {
  if ($num > $largest) {
    $largest = $num;
  }
}
// Find the smallest number
$smallest = $numbers[0];
foreach ($numbers as $num) {
  if ($num < $smallest) {
    $smallest = $num;
  }
}
echo "The largest number is: " . $largest . "\n";
echo "The smallest number is: " . $smallest;
?>

Output:

The largest number is: 9
The smallest number is: 1
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send