How to add serial

Problem: How to add numbers between 1 to 100?

This obey arithmetic sequence. Mathematical formula wise this is 100*(1+100)/2=1050. Here is details.

Let see how to implement this in PHP.

function sum( $n, $a1, $a2 ) {
    return $n * ( $a1 + $a2 ) / 2;
}
echo 'Total=' . sum( 100, 1, 100 );

Leave a Reply

Your email address will not be published. Required fields are marked *