w3resource

C Exercises: Create maximum number of regions obtained by drawing n given straight lines


Maximum regions formed by nnn straight lines

Write a C program to create the maximum number of regions obtained by drawing n given straight lines.

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 four areas.

Input:
(1 ≤ n ≤ 10,000)

C Programming: Create maximum number of regions obtained by drawing n given straight lines.

Sample Solution:

C Code:

#include <stdio.h>

int main() {
    long n;

    // Prompt user to input number of straight lines
    printf("Input number of straight lines:\n");
    scanf("%ld", &n);

    // Calculate and display maximum number of regions obtained by drawing 'n' given straight lines
    printf("Maximum number of regions obtained by drawing %ld given straight lines:\n", n);
    printf("%ld\n", (n*n+n+2)/2);

    return 0;
}

Sample Output:

Input number of straight lines:
2
Maximum number of regions obtained by drawing 2 given straight lines:
4

Flowchart:

C Programming Flowchart: Create maximum number of regions obtained by drawing n given straight lines.

For more Practice: Solve these Related Problems:

  • Write a C program to calculate the maximum number of regions created by n straight lines using the formula n(n+1)/2 + 1.
  • Write a C program to compute regions by simulating line addition and updating region counts iteratively.
  • Write a C program to use recursion to derive the maximum regions formed by drawing n lines.
  • Write a C program to verify the formula for maximum regions by comparing computed values for increasing n.

C programming Code Editor:



Previous: Write a C program to find the difference between the largest integer and the smallest integer, which are created by 8 numbers from 0 to 9. The number that can be rearranged shall start with 0 as in 00135668.
Next: Write a C program to sum of all numerical values (positive integers) embedded in a sentence.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.