Q:

If you draw a straight line on a plane, the plane is divided into two regions. For example, if you pull two straight lines in parallel,

0

If you draw a straight line on a plane, the plane is divided into two regions. For example, if you pull two straight lines in parallel, you get three areas, and if you draw vertically one to the other you get 4 areas.
Write a PHP program to create maximum number of regions obtained by drawing n given straight lines.

Input: (1 ≤ n ≤ 10,000)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
while (($line = trim(fgets(STDIN))) !== '') {
    $n = (int)$line;
    $result = 2;
    for ($i = 2; $i <= $n; $i++) {
        $result += $i;
    } 
    echo 'Maximum number of regions: ';
    echo $result, PHP_EOL;
}
?>

Sample Input:
5

Sample Output:

Maximum number of regions: 16

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now