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.

Respuesta :

Answer:

Solution a

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

Solution b

  1. n = int(input("Enter an integer: "))
  2. for a in range(1, n + 1):
  3.    sum = 0
  4.    for x in range(1, a+1):
  5.        sum += x
  6.    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).

The program that prompts for an integer—let’s call it X—and then finds the sum of X consecutive integers starting at 1 is represented as follows:

x = int(input("please input an integer: "))

all_numbers = []

for i in range(1, x+1):

  all_numbers.append(i)

print(sum(all_numbers))

The first line of code ask the user for an integer input.

The second line of code is a variable named all_numbers initialised with an empty list.

Then we loop through the range number 1 to the inputted integer plus one.

Then append the looped value to the empty array.

Finally, we print the sum of the already filled array.

learn more on python here; https://brainly.com/question/13855711?referrer=searchResults

Ver imagen vintechnology
ACCESS MORE
EDU ACCESS
Universidad de Mexico