Basic Programs

If Else Programs

While loop Programs

For loop Programs

List Programs


Python Program to Find the Sum of all Numbers from 1 to a Given Number using For Loop


This examples shows, how to calculate the sum of all numbers from 1 to a given number using for loop

Steps
  1. Get the input from user.
  2. Set the variable(sum) store the sum value.
  3. Increment given by 1 in the for loop.Print the numbers 1 to given limit and also sum of numbers.
Example:
Example.py
limit = int(input("Enter a number: "))
sum = 0
for i in range(1,limit+1):
    print(i)
    sum+=i
print("The sum of Given Limit from 1 to {0} = {1}".format(limit,sum))

Output

Enter a number: 10
1
2
3
4
5
6
7
8
9
10
The sum of Given Limit from 1 to 10 = 55

Prev Next