Respuesta :
Answer:
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).
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
