Write a program that asks the user for two numbers. Using a for loop, add all of the numbers from the first to the second. For example if the first number is 6 and the second number is 8 the result is 21 (6 + 7 + 8).
Write a program that asks the user for two numbers. Using a for loop, add all of the numbers from the first to the second. For example if the first number is 6 and the second number is 8 the result is 21 (6 + 7 + 8)
Share
Here’s a simple program in Python that accomplishes what you’ve described:
# Ask the user for two numbers
first_number = int(input("Enter the first number: "))
second_number = int(input("Enter the second number: "))
# Initialize a variable to hold the sum
total_sum = 0
# Use a for loop to add all numbers from first_number to second_number
for number in range(first_number, second_number + 1):
total_sum += number
# Print the result
print("The sum is:", total_sum)
### Explanation:
1. The program prompts the user to input two numbers.
2. It initializes `total_sum` to store the cumulative sum.
3. A `for` loop iterates through each number from `first_number` to `second_number` (inclusive).
4. Each number is added to `total_sum`.
5. Finally, the program displays the total sum.
Feel free to ask more questions if you need further assistance!