Python Program to Print the Natural Numbers Summation Pattern

This is a Python Program to read a number n and print the natural numbers summation pattern.

Problem Description

The program takes a number n and prints the natural numbers summation pattern.

Problem Solution

1. Take a value from the user and store it in a variable n.
2. Use two for loop where the value of j ranges between the values of 1 and n and value of i ranges between 1 and j.
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 the natural numbers summation pattern. The program output is also shown below.

n=int(input("Enter a number: "))
for j in range(1,n+1):
    a=[]
    for i in range(1,j+1):
        print(i,sep=" ",end=" ")
        if(i<j):
            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 outer for loop enables j to range between 1 and n (as n+1 is not included) while the inner for loop enables i to range between 1 and i.
3. For each iteration, the value of i is printed.
4. ‘+’ operator is printed only if i

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

Sanfoundry Global Education & Learning Series – Python Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.