In the previous article, we have discussed Python Program to Print Series 1 2 5 8 15 28 51 94….N
Given a number N and the task is to print the series (0 2 6 12 20 30 42…N) till the given number N in Python.
Examples:
Example1:
Input:
Given Number (Limit) = 9
Output:
The above series till the given number{ 9 } is :
0 2 6 12 20 30 42 56 72Example2:
Input:
Given Number (Limit) = 10
Output:
The above series till the given number{ 10 } is :
0 2 6 12 20 30 42 56 72 90Program to Print Series 0 2 6 12 20 30 42…N in Python
Below are the ways to print the series (0 2 6 12 20 30 42…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.
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Multiply the above itr value with itself and subtract again the itr value from it.
- Store it in another variable.
- Print the value of the above result separated by spaces using the end function.
- 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 = 9
# Take a variable to say itr and initialize its value to 1.
itr = 1
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:
# Multiply the above itr value with itself and subtract again the itr value from it.
# Store it in another variable.
k = (itr*itr)-itr
# Print the value of the above result separated by spaces using the end function.
print(k, end=" ")
# Increment the above itr value by 1.
itr += 1
Output:
The above series till the given number{ 9 } is :
0 2 6 12 20 30 42 56 72Method #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.
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Multiply the above itr value with itself and subtract again the itr value from it.
- Store it in another variable.
- Print the value of the above result separated by spaces using the end function.
- 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
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:
# Multiply the above itr value with itself and subtract again the itr value from it.
# Store it in another variable.
k = (itr*itr)-itr
# Print the value of the above result separated by spaces using the end function.
print(k, 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 :
0 2 6 12 20 30 42 56 72 90Practice Python Program Examples to master coding skills and learn the fundamental concepts in the dynamic programming language Python.
