PHP Program to find Average of N Numbers

Last Updated:

Using for loop

In the following example, we will find the average of numbers between 1 and 10 using for loop.

Example

PHP Compiler
<?php \$start = 1; \$end = 10; \$total = 0; \$count = (\$end - \$start) + 1; \$average = 0; echo "Average of numbers between \$start and \$end: <br>"; for(\$start=\$start; \$start<=\$end; \$start++) \$total += \$start; echo "<br> Total = \$total"; echo "<br> Count = \$count"; \$average = \$total / \$count; echo "<br> Average is \$average"; ?>

Using while loop

In the following example, we will find the average of numbers between 1 and 10 using while loop.

Example

PHP Compiler
<?php \$start = 1; \$end = 10; \$total = 0; \$count = (\$end - \$start) + 1; \$average = 0; echo "Average of numbers between \$start and \$end: <br>"; while(\$start <= \$end) { \$total += \$start; \$start++; } echo "<br> Total = \$total"; echo "<br> Count = \$count"; \$average = \$total / \$count; echo "<br> Average is \$average"; ?>

Using do while loop

In the following example, we will find the average of numbers between 1 and 10 using do while loop.

Example

PHP Compiler
<?php \$start = 1; \$end = 10; \$total = 0; \$count = (\$end - \$start) + 1; \$average = 0; echo "Average of numbers between \$start and \$end: <br>"; do{ \$total += \$start; \$start++; }while(\$start <= \$end); echo "<br> Total = \$total"; echo "<br> Count = \$count"; \$average = \$total / \$count; echo "<br> Average is \$average"; ?>