Previous

While Loops

2 / 4
Next

A while loop can be used to calculate the sum of integers from 1 to n.

n = int(input())
sum = 0
i = 1
while i <= n:
  sum += i
  i += 1
print(sum)

Here sum += i is shorthand notation for sum = sum + i, adding the current value to the total sum. Similarly, i += 1 is the same as i = i + 1 and increments i with 1.

Send

Factorial

Hint
Beginner

Input an integer n. Compute and print the factorial of n.

Hint: the factorial of n is the product of all positive integers up to n. For example, if n is 5, the factorial is 5 * 4 * 3 * 2 * 1 = 120.

Help
Solve
Reset
Run
Submit
 

Test results

Test result #
Expected output
Your output

Your email has been verified!