Write a PHP script to compute the sum and average of n
numbers.
Answers
Answer:
jhhhjfy egg ffdgfsgdfsfdgdvcdfbbhjjjhfgdfetdfdfd
Answer:
This program is used to display or find sum or addition of numbers present in array and finding average of numbers or integers present in an array.
Programming Logic:
Get the sum of integers present in an array.
Count the number of integers present in the array.
Divide sum of integers with number of integers present in the array.
Display the result
Explanation:
FIND SUM & AVERAGE OF NUMBERS IN ARRAY MODEL 1:
<?php
$a = array(1,2,3,4,5); // array declaration
$b = count($a); // counting array elements
$c = array_sum($a); // addition of array integers
echo "Number of integers present in array are: ", $b ,"<br>";
echo "Sum of integers present in array is: ", $c ,"<br>";
$d = $c/$b;
echo "Average of array numbers is: ",$d;
?>
Output:
Number of integers present in array are: 5
Sum of integers present in array is: 15
Average of array numbers is: 3
FIND SUM & AVERAGE OF NUMBERS IN ARRAY MODEL 2:
<?php
$res = 0;
$a = array(1,2,3,4,5); // array declaration
for($i=0;$i<count($a);$i++)
{
$res += $a[$i]; // a = a + b;
$d = $res/count($a);
}
echo "Avg of array numbers is: ",$d;
?>
Output:
Avg of array numbers is: 3