Write a program using Python that prompts for an integer—let’s call it X—and the
ID: 669844 • Letter: W
Question
Write a program using Python that prompts for an integer—let’s call it X—and then nds the sum of X consecutive integers starting at 1. That is, if X = 5, you will nd the sum of 1 + 2 + 3 + 4 + 5 = 15
Modify your program by enclosing your loop in another loop so that you can nd consecutive sums. For example, if 5 is entered, you will nd ve sums of consecutive numbers:
1 = 1
1+2 = 3
1+2+3 = 6
1+2+3+4 = 10
1+2+3+4+5 = 15
Print only each sum, not the arithmetic expression.
Explanation / Answer
ANSWER:
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
sum = 0
i=1
j=0
# use while loop to iterate un till zero
while(i <= num):
j=i
while(j > 0):
sum= sum+j
j=j-1
print ('The sum = ',sum)
i=i+1
sum=0
OUTPUT:
Enter a number: 5
('The sum = ', 1)
('The sum = ', 3)
('The sum = ', 6)
('The sum = ', 10)
('The sum = ', 15)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.