In the previous article, we have discussed Python Program to Print Series 1, -2, 6, -15, 31 … N
Given a number N and the task is to print the series (6, 9, 14, 21, 30, 41, 54 … N) till the given number N in Python.
Examples:
Example1:
Input:
Given Number (Limit) = 7
Output:
The above series till the given number{ 7 } is :
6 9 14 21 30 41 54Example2:
Input:
Given Number (Limit) = 10
Output:
The above series till the given number{ 10 } is :
6 9 14 21 30 41 54 69 86 105Program to Print Series 6, 9, 14, 21, 30, 41, 54 … NÂ in Python
Below are the ways to print the series (6, 9, 14, 21, 30, 41, 54 … 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 1.
- Take another variable say k and initialize its value to 3.
- Take another variable say valu and initialize its value to 6.
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Inside the loop, print the value of the above variable valu separated by spaces.
- Add k value to the above valu and store it in the same variable valu.
- Increment the above k value by 2.
- 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 = 7
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say k and initialize its value to 3.
k = 3
# Take another variable say valu and initialize its value to 6.
valu = 6
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 loop, print the value of the above variable valu separated by spaces.
print(valu, end=" ")
# Add k value to the above valu and store it in the same variable valu.
valu += k
# Increment the above k value by 2.
k += 2
# Increment the above itr value by 1.
itr += 1
Output:
The above series till the given number{ 7 } is :
6 9 14 21 30 41 54Method #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 1.
- Let us Take another variable say k and initialize its value to 3.
- Take another variable say valu and initialize its value to 6.
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Inside the loop, print the value of the above variable valu separated by spaces.
- Add k value to the above valu and store it in the same variable valu.
- Increment the above k value by 2.
- 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 1.
itr = 1
# Take another variable say k and initialize its value to 3.
k = 3
# Take another variable say valu and initialize its value to 6.
valu = 6
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 loop, print the value of the above variable valu separated by spaces.
print(valu, end=" ")
# Add k value to the above valu and store it in the same variable valu.
valu += k
# Increment the above k value by 2.
k += 2
# Increment the above itr value by 1.
itr += 1
Output:
Enter some Random Number = 10
The above series till the given number{ 10 } is :
6 9 14 21 30 41 54 69 86 105Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.
