In this python basic program, we will find the sum of natural numbers defined by the user.
Steps to solve the program
- Take the last number up to which you want to find the sum as input through the user.
- Create a variable to store the sum and assign its value equal to 0.
- Use for loop with the range function to iterate over natural numbers defined by the user.
- During iteration add the respective natural number to the variable that we have created to find the sum.
- Print the output.
num = int(input("Enter a number up to which you want to find sum: "))
total = 0
for i in range(1,num+1):
total += i
print("Total: ",total)
Output :
Enter a number up to which you want to find sum: 10
Total: 55