Python Program to Read a Number n and Print the Series “1+2+…..+n= “

This is a Python Program to read a number n and print and compute the series “1+2+…+n=”.

Problem Description

The program takes a number n and prints and computes the series “1+2+…+n=”.

Problem Solution

1. Take a value from the user and store it in a variable n.
2. Use a for loop where the value of i ranges between the values of 1 and n.
3. Print the value of i and ‘+’ operator while appending the value of i to a list.
4. Then find the sum of elements in the list.
5. Print ‘=’ followed by the total sum.
6. Exit.

Program/Source Code

Here is the source code of the Python Program to read a number n and print and compute the series “1+2+…+n=”. The program output is also shown below.

n=int(input("Enter a number: "))
a=[]
for i in range(1,n+1):
    print(i,sep=" ",end=" ")
    if(i<n):
        print("+",sep=" ",end=" ")
    a.append(i)
print("=",sum(a))
 
print()
Program Explanation

1. User must first enter the value and store it in a variable n.
2. The for loop enables i to range between 1 and n (as n+1 is not included).
3. For each iteration, the value of i is printed.
4. ‘+’ operator is printed only if i

Runtime Test Cases
advertisement
 
Case 1:
Enter a number: 4
1 + 2 + 3 + 4 = 10
 
Case 2:
Enter a number: 5
1 + 2 + 3 + 4 + 5 = 15

Sanfoundry Global Education & Learning Series – 1000 Python Programs.

⚡ Hurry! Secure Your Free Cyber Security Certification - January 2026

If you wish to look at all Python Programming examples, go to 1000 Python Programs.

👉 For weekly programming practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels

advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations