Exercise 8
Write a python algorithm that asks the user to enter an integer n and displays the value of the sum 1 + 2 + … + n = ?
Solution
1 2 3 4 5 6 7 8 |
# Ask the user to enter the value of the integer n n = int(input("Type a value of the integer n ")) # define and initialize the sum s = 0 for i in range(1,n+1): s = s + i print("The sum of 1 + 2 + 3 + ...+ ",n," = : ", s) |
Younes Derfoufi
CRMEF OUJDA
1 thought on “Solution Exercise 8: calculating the sum of the first numbers in Python”