# Python: Create maximum number of regions obtained by drawing n given straight lines

## Python Basic - 1: Exercise-54 with Solution

If you draw a straight line on a plane, the plane is divided into two regions. For example, if you draw two straight lines in parallel, you get three areas, and if you draw vertically one to the other you get 4 areas.

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

**Input:**

(1 ≤ n ≤ 10,000)

Input number of straight lines (o to exit):

5

Number of regions:

16

**Sample Solution**:

**Python Code:**

```
# Infinite loop to continuously prompt the user for input
while True:
# Print statement to prompt the user to input the number of straight lines (0 to exit)
print("Input number of straight lines (0 to exit): ")
# Take user input for the number of straight lines
n = int(input())
# Check if the entered number is less than or equal to 0, and break the loop if true
if n <= 0:
break
# Print statement to display the number of regions
print("Number of regions:")
# Calculate and print the number of regions using the given formula
print((n*n + n + 2)//2)
```

Sample Output:

Input number of straight lines (o to exit): 5 Number of regions: 16

**Explanation:**

Here is a breakdown of the above Python code:

- The code enters an infinite loop using while True.
- Inside the loop, the user is asked to input the number of straight lines or 0 to exit.
- It takes user input for the number of straight lines (n).
- It checks if 'n' is less than or equal to 0, and if true, breaks out of the loop.
- If 'n' is greater than 0, it calculates the number of regions using the given formula and prints the result.

**Flowchart:**

**Python Code Editor:**

**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**

**Previous:** Write a Python program that accept a even number (>=4, Goldbach number) from the user and create a combinations that express the given number as a sum of two prime numbers. Print the number of combinations.

**Next:** Write a Python program to test AB and CD are orthogonal or not.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics