In the previous article, we have discussed Python Program to Print Series 1, 2, 4, 8, 16, 32…n
Given a number N and the task is to print the series (6,11,21,36,56…N) for the given number N in Python.
Examples:
Example1:
Input:
Given Number = 6
Output:
The above series till the given number{ 6 } is : 6 11 21 36 56 81
Example2:
Input:
Given Number = 10
Output:
The above series till the given number{ 10 } is : 6 11 21 36 56 81 111 146 186 231
Program to Print Series 6,11,21,36,56…n in Python
Below are the ways to print the series (6,11,21,36,56…N) for the given number N in Python:
Method #1: Using While Loop (Static Input)
Approach:
- Give the number N as static input and store it in a variable.
- Take a variable to say itr and initialize its value to 1.
- Take another variable to say previos_val and initialize its value to 6.
- Take the other variable to say gvn_diffence and initialize its value to 5 (which is the common difference).
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Inside the while loop, print the value of the above previos_val value separated by spaces.
- Add the given difference to the previous value and store it in the same variable previos_val.
- Add 5 to the given difference and store it in the same variable.
- Increment the value of the above itr value by 1 and store it in the same variable.
- The Exit of the Program.
Below is the implementation:
# Give the number N as static input and store it in a variable. gvn_numb = 6 # Take a variable to say itr and initialize its value to 1. itr = 1 # Take another variable to say previos_val and initialize its value to 6. previos_val = 6 # Take the other variable to say gvn_diffence and initialize its value to 5 # (which is the common difference). gvn_diffence = 5 print("The above series till the given number{", gvn_numb, "} is :") # Loop until the above-declared variable itr value is less than or equal to the # given number using the while loop. while itr <= gvn_numb: # Inside the while loop, print the value of the above previos_val value # separated by spaces. print(previos_val, end=" ") # Add the given difference to the previous value and store it in the same # variable previos_val. previos_val = previos_val + gvn_diffence # Add 5 to the given difference and store it in the same variable. gvn_diffence = gvn_diffence + 5 # Increment the value of the above itr value by 1 and store it in the # same variable. itr += 1
Output:
The above series till the given number{ 6 } is : 6 11 21 36 56 81
Method #2: Using While loop (User Input)
Approach:
- Give the number N 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 1.
- Take another variable to say previos_val and initialize its value to 6.
- Take the other variable to say gvn_diffence and initialize its value to 5 (which is the common difference).
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Inside the while loop, print the value of the above previos_val value separated by spaces.
- Add the given difference to the previous value and store it in the same variable previos_val.
- Add 5 to the given difference and store it in the same variable.
- Increment the value of the above itr value by 1 and store it in the same variable.
- The Exit of the Program.
Below is the implementation:
# Give the number N 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 1. itr = 1 # Take another variable to say previos_val and initialize its value to 6. previos_val = 6 # Take the other variable to say gvn_diffence and initialize its value to 5 # (which is the common difference). gvn_diffence = 5 print("The above series till the given number{", gvn_numb, "} is :") # Loop until the above-declared variable itr value is less than or equal to the # given number using the while loop. while itr <= gvn_numb: # Inside the while loop, print the value of the above previos_val value # separated by spaces. print(previos_val, end=" ") # Add the given difference to the previous value and store it in the same # variable previos_val. previos_val = previos_val + gvn_diffence # Add 5 to the given difference and store it in the same variable. gvn_diffence = gvn_diffence + 5 # Increment the value of the above itr value by 1 and store it in the # same variable. itr += 1
Output:
Enter some Random Number = 10 The above series till the given number{ 10 } is : 6 11 21 36 56 81 111 146 186 231
If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples existing and learn the approach used.