Python Program to Print Series 2, 4, 7, 12, 21, … N

In the previous article, we have discussed Python Program to Print Series 2 ,15, 41, 80…n
Given a number N and the task is to print the series (2, 4, 7, 12, 21, … N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number (Limit) = 8

Output:

The above series till the given number{ 8 } is :
2 4 7 12 21 38 71 136

Example2:

Input:

Given Number (Limit) = 10

Output:

The above series till the given number{ 10 } is :
2 4 7 12 21 38 71 136 265 522

Program to Print Series 2 4 7 12 21 … N in Python

Below are the ways to print the series (2, 4, 7, 12, 21, … N) till the given number N in Python:

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N (Limit) as static input and store it in a variable.
  • Take a variable to say itr and initialize its value to 0.
  • Take another variable say previous_val and initialize its value to 2.
  • Print the value 2 separated by spaces.
  • Loop until the above-declared variable itr value is less than the given number-1 using the while loop.
  • Inside the loop, Multiply the variable previous_val with 2 and subtract the above itr value from it.
  • Store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N(limit) as static input and store it in a variable.
gvn_numb = 8
# Take a variable to say itr and initialize its value to 0.
itr = 0
# Take another variable say previous_val and initialize its value to 2.
previous_val = 2
print("The above series till the given number{", gvn_numb, "} is :")
# print the value 2 separated by spaces.
print("2 ",end="")
# Loop until the above-declared variable itr value is less than the
# given number-1 using the while loop.
while itr<gvn_numb-1:
    #Inside the loop, Multiply the variable previous_val with 2 and subtract the above
    #itr value from it.
    #Store it in the same variable previous_val.
    previous_val = (previous_val * 2) -itr
    # Print the value of the above previous_val separated by spaces.
    print(previous_val,end=" ")
    # Increment the above itr value by 1.
    itr+=1

Output:

The above series till the given number{ 8 } is :
2 4 7 12 21 38 71 136

Method #2: Using While loop (User Input)

Approach:

  • Give the number N (Limit) as user input using the int(input()) function and store it in a variable.
  • Take a variable to say itr and initialize its value to 0.
  • Take another variable say previous_val and initialize its value to 2.
  • Print the value 2 separated by spaces.
  • Loop until the above-declared variable itr value is less than the given number-1 using the while loop.
  • Inside the loop, Multiply the variable previous_val with 2 and subtract the above itr value from it.
  • Store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N (Limit) as user input using the int(input()) function and 
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable to say itr and initialize its value to 0.
itr = 0
# Take another variable say previous_val and initialize its value to 2.
previous_val = 2
print("The above series till the given number{", gvn_numb, "} is :")
# print the value 2 separated by spaces.
print("2 ",end="")
# Loop until the above-declared variable itr value is less than the
# given number-1 using the while loop.
while itr<gvn_numb-1:
    #Inside the loop, Multiply the variable previous_val with 2 and subtract the above
    #itr value from it.
    #Store it in the same variable previous_val.
    previous_val = (previous_val * 2) -itr
    # Print the value of the above previous_val separated by spaces.
    print(previous_val,end=" ")
    # Increment the above itr value by 1.
    itr+=1

Output:

Enter some Random Number = 10
The above series till the given number{ 10 } is :
2 4 7 12 21 38 71 136 265 522

Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.