Ask Question
18 April, 21:22

a) Write a program that prompts for an integer-let's call it X-and then finds the sum of X consecutive integers starting at 1. That is, if X=5, you will find the sum of 1+2+3+4+5=15. b) Modify your program by enclosing your loop in another loop so that you can find consecutive sums. For example, if 5 is entered, you will find five sum of consecutive numbers:1 = 11+2 = 31+2+3 = 61+2+3+4 = 101+2+3+4+5 = 15Print only each sum, not the arithmetic expression.

+4
Answers (1)
  1. 18 April, 23:24
    0
    Solution a

    n = int (input ("Enter an integer: ")) sum = 0 for x in range (1, n+1) : sum + = x print (sum)

    Solution b

    n = int (input ("Enter an integer: ")) for a in range (1, n + 1) : sum = 0 for x in range (1, a+1) : sum + = x print (sum)

    Explanation:

    Solution code is written in Python 3.

    Solution a

    First get the user input for an integer (Line 1).

    Create a variable sum and initialize it with zero value (Line 3).

    Create a for loop to traverse through the number from 1 to integer n (inclusive) and sum up the current number x (Line 5-6).

    Print the sum to terminal (Line 8).

    Solution b

    Create an outer for loop that traverse through the number from 1 to integer n (inclusive) (Line 3).

    Create an inner loop that traverse through the number from 1 to current a value from the outer loop and sum up the current x value (Line 5-6).

    Print the sum to terminal (Line 7) and proceed to next iteration and reset the sum to zero (Line 4).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “a) Write a program that prompts for an integer-let's call it X-and then finds the sum of X consecutive integers starting at 1. That is, if ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers