The following program would ask the user to enter numbers when executed and the average of the numbers is shown as the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
$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);
|