Exercise 4: Summation ∑

➕ Operators 🔂 Loops

Your Task

Write a program that asks the user for a number n, then computes and prints the sum:

i=1n1i=1+2+3++(n1)\sum_{i=1}^{n-1} i = 1 + 2 + 3 + \cdots + (n-1)

Example: When n = 10:

i=19i=45\sum_{i=1}^{9} i = 45

Use a loop (or the formula (n-1) * n // 2) to compute it.


Example Output

Enter n: 10
45

Try it yourself

Code: Summation

Loading Python runtime…
Python
Loading...
bash
$ Click "Run" to execute your code...