Python Program to Print the Natural Numbers Summation Pattern

Python Program to Print the Natural Numbers Summation Pattern

Natural Numbers:

Natural numbers are a subset of the number system that includes all positive integers from one to infinity. Natural numbers, which do not include zero or negative numbers, are also known as counting numbers. They are a subset of real numbers that include only positive integers and exclude zero, fractions, decimals, and negative numbers.

Given a number the task is to print the natural numbers summation Pattern

Examples:

Example1:

Input:

given number = 7

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

Example2:

Input:

given number = 9

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

Program to Print the Natural Numbers Summation Pattern in Python

There are several ways to print the natural numbers summation pattern in Python some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1: Using nested for loops (Static Input)

Approach:

  • Take the value of the given number as static  and store it in a variable numb.
  • Use two for loops where the value of i is between 1 and numb and the value of j is between 1 and i
  • Take a empty list
  • Add values of i to the list using append() function.
  • While adding the value of j to a list, print the value of j and the ‘+’ operator.
  • Then compute the sum of the elements in the list using sum() function
  • Print the symbol ‘=’ followed by the total sum calculated in the above step.
  • End of program

Below is the implementation:

# given number
numb = 15
# traversing from 1 to numb using for loop
for i in range(1, numb+1):
  # taking a empty list
    emptylist = []
    # traversing from 1 to i
    for j in range(1, i+1):
      # printing the value of j
        print(j, sep=" ", end=" ")
        if(j < i):
            print("+", sep=" ", end=" ")
        # appending/adding the value to the empty list
        emptylist.append(j)
    # printing the sum of emptylist
    print("=", sum(emptylist))

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
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 = 66
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 = 78
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 = 91
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 = 105
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 = 120

Explanation:

  • Consider the provided number’s value to be static
  • This number is iterated again and over.
  • An empty list has been defined.
  • A new ‘for’ loop is started.
  • The separator is set to ‘’.
  • The ‘+’ symbol is used if the value of the inner iterator is smaller than the value of the outer iterator.
  • These values are added to the existing empty list.
  • Their total is computed and shown as the output.

Method #2: Using nested for loops and mathematical formula (Static input)

Approach:

  • Take the value of the given number as static and store it in a variable numb.
  • Use two for loops where the value of i is between 1 and numb and the value of j is between 1 and i
  • print the value of j and the ‘+’ operator.
  • Then compute the sum of the element i using mathematic formula for sum of n natural numbers
  • Print the symbol ‘=’ followed by the total sum calculated in the above step.
  • End of program

Mathematical formula for sum of n natural numbers  :

( numb * (numb+1) ) /2

Below is the implementation:

# given number
numb = 9
# traversing from 1 to numb using for loop
for i in range(1, numb+1):
    # traversing from 1 to i
    for j in range(1, i+1):
      # printing the value of j
        print(j, sep=" ", end=" ")
        if(j < i):
            print("+", sep=" ", end=" ")
    # calculating sum of the n natural numbers using mathematical formula
    naturalSum = (i*(i+1))//2
    # printing the total sum
    print("=", naturalSum)

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

Method #3: Using nested for loops and mathematical formula (User input)

Approach:

  • Scan the input from user using input() function and store it in a variable numb
  • Convert this numb to integer using numb function
  • Use two for loops where the value of i is between 1 and numb and the value of j is between 1 and i
  • print the value of j and the ‘+’ operator.
  • Then compute the sum of the element i using mathematic formula for sum of n natural numbers
  • Print the symbol ‘=’ followed by the total sum calculated in the above step.
  • End of program

Mathematical formula for sum of n natural numbers  = ( numb * (numb+1) ) /2

Below is the implementation:

# given number
numb = input("Enter your desired number = ")
# conveerting numb to integer using int() function
numb = int(numb)
# traversing from 1 to numb using for loop
for i in range(1, numb+1):
    # traversing from 1 to i
    for j in range(1, i+1):
      # printing the value of j
        print(j, sep=" ", end=" ")
        if(j < i):
            print("+", sep=" ", end=" ")
    # calculating sum of the n natural numbers using mathematical formula
    naturalSum = (i*(i+1))//2
    # printing the total sum
    print("=", naturalSum)

Output:

Enter your desired number = 9
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

Related Programs: