Question:

Write the program to process a list of numbers.

Answer:

The following program would ask the user to enter numbers when executed and the average of the numbers is shown as the output:

$sum = 0;
 
$count = 0;
 
print "Enter number: ";
 
$num = <>;
 
chomp($num);
 
while ($num >= 0)
 
{
 
$count++;
 
$sum += $num;
 
print "Enter another number: ";
 
$num = <>;
 
chomp($num);
 
}
 
print "$count numbers were entered\n";
 
if ($count > 0)
 
{
 
print "The average is ",$sum/$count,"\n";
 
}
 
exit(0);

 


Keywords:

© 2017 QuizBucket.org