114k views
0 votes
Write a for loop that reads an integer from the user, and prints the sum of numbers from 1 to that integer (inclusive). If the user enters a number less than 1, he/she should be prompted to enter a number greater or equal to 1.

User HaleFx
by
5.7k points

1 Answer

2 votes

Answer:

Written in Python

num = int(input("Number: "))

while num < 1:

num = int(input("Number: "))

total = 0

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

total= total + i

print("Total: "+str(total))

Step-by-step explanation:

This line prompts user for input

num = int(input("Number: "))

The following iteration checks and prompts user for valid input

while num < 1:

num = int(input("Number: "))

This initializes total to 0

total = 0

The following iteration adds from 1 to input number

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

total= total + i

This displays the total

print("Total: "+str(total))

User Rob Van Groenewoud
by
5.9k points