Skip to content
Related Articles
Open in App
Not now

Related Articles

Python Program to Print the Natural Numbers Summation Pattern

Improve Article
Save Article
  • Last Updated : 04 Jun, 2022
Improve Article
Save Article

Given a natural number n, the task is to write a Python program to first find the sum of first n natural numbers and then print each step as a pattern.

Input: 5

Output:

1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15

Input: 10

Output:

1 = 1

1 + 2 = 3

1 + 2 + 3 = 6

1 + 2 + 3 + 4 = 10

1 + 2 + 3 + 4 + 5 = 15

1 + 2 + 3 + 4 + 5 + 6 = 21

1 + 2 + 3 + 4 + 5 + 6 + 7 = 28

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

Approach:

  • Take Input n.
  • Use two loops:
    • j ranging between 1 to n.
    • i ranging between 1 to j.
  • Print the value of i and ‘+’ operator while appending the value of i to a list.
  • Then Find the Sum of Elements in the List.
  • Print “=” with Total Sum.
  • Exit.

Program:

Python3




# Inputing Natural Number
number = int(input("Enter the Natural Number: "))
 
# j ranges from 1 to n
for j in range(1, number+1):
 
    # Initializing List
    a = []
 
    # i loop ranges from 1 to j
    for i in range(1, j+1):
        print(i, sep=" ", end=" ")
        if(i < j):
            print("+", sep=" ", end=" ")
        a.append(i)
    print("=", sum(a))
 
print()

Output:

Enter the Natural Number: 5

1 = 1

1 + 2 = 3

1 + 2 + 3 = 6

1 + 2 + 3 + 4 = 10

1 + 2 + 3 + 4 + 5 = 15

Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!