In the previous article, we have discussed Python Program to Print Series 1 9 17 33 49 73 97 …N
Given a number N (Limit) and the task is to print the series ( 2,15,41,80…N) till the given number N in Python.
Examples:
Example1:
Input:
Given Number(limit) = 15
Output:
The above series till the given number{ 15 } is : 2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367
Example2:
Input:
Given Number(limit) = 20
Output:
The above series till the given number{ 20 } is : 2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367 1562 1770 1991 2225 2472
Program to Print Series 2,15,41,80…n in Python
Below are the ways to print the series ( 2,15,41,80…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 valu and initialize its value to 2.
- 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.
- Multiply the value of itr with 13 and add the result to the valu.
- Store the above result in the same variable valu.
- 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 = 15 # Take a variable to say itr and initialize its value to 1. itr = 1 # Take another variable say valu and initialize its value to 2. valu=2 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=" ") #Multiply the value of itr with 13 and add the result to the valu. #Store the above result in the same variable valu. valu+=itr*13 #Increment the above itr value by 1. itr+=1
Output:
The above series till the given number{ 15 } is : 2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367
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 1.
- Take another variable say valu and initialize its value to 2.
- 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.
- Multiply the value of itr with 13 and add the result to the valu.
- Store the above result in the same variable valu.
- 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 valu and initialize its value to 2. valu=2 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=" ") #Multiply the value of itr with 13 and add the result to the valu. #Store the above result in the same variable valu. valu+=itr*13 #Increment the above itr value by 1. itr+=1
Output:
Enter some Random Number = 20 The above series till the given number{ 20 } is : 2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367 1562 1770 1991 2225 2472
Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.