In the previous article, we have discussed Python Program to Print Series 0, 6, 10, 17, 22, 30, 36…N. Along with that 1 2 5 8 15 Series Logic, Series Program In Python, Print Series In Python, Number Series In Python, 1 3 4 8 15 Series Program, Number Series Program In Python, Series Questions In Python, In Python 3 What Is The Output Of 1/2.
Given a number N and the task is to print the series (1 2 5 8 15 28 51 94….N) till the given number N in Python.
Examples:
Example1:
Input:
Given Number (Limit) = 13
Output:
The above series till the given number{ 13 } is : 1 2 5 8 15 28 51 94 173 318 585 1076 1979
Example2:
Input:
Given Number (Limit) = 8
Output:
The above series till the given number{ 8 } is : 1 2 5 8 15 28 51 94
- Python Program to Print Series 0 2 6 12 20 30 42…N
- Python Program to Find Sum of Series 5^2 + 10^2 + 15^2 +…..N^2
- Python Program to Print Series 6, 9, 14, 21, 30, 41, 54 … N
Program to Print Series 1 2 5 8 15 28 51 94….N in Python
Below are the ways to print the series (1 2 5 8 15 28 51 94….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 4.
- Check if the given number is greater than or equal to 1 using the if conditional statement.
- If it is true, then print 1 separated by spaces using the end function.
- Check if the given number is greater than or equal to 2 using the if conditional statement.
- If it is true, then print 2 separated by spaces using the end function.
- Check if the given number is greater than or equal to 3 using the if conditional statement.
- If it is true, then print 5 separated by spaces using the end function.
- Take another variable say m and initialize its value to 1.
- Take another variable say n and initialize its value to 2.
- Take another variable say o and initialize its value to 5.
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Inside the loop, add the above 3 values m, n, o and store them in a variable say rslt.
- Assign the value of n to m.
- Assign the value of o to n.
- Assign the value of rslt to o.
- Print the value of the rslt 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 = 13 # Take a variable to say itr and initialize its value to 4. itr = 4 print("The above series till the given number{", gvn_numb, "} is :") # Check if the given number is greater than or equal to 1 using the if conditional # statement. if gvn_numb >= 1: # If it is true, then print 1 separated by spaces using the end function. print("1 ", end="") # Check if the given number is greater than or equal to 2 using the if conditional # statement. if gvn_numb >= 2: # If it is true, then print 2 separated by spaces using the end function. print("2 ", end="") # Check if the given number is greater than or equal to 3 using the if conditional # statement. if gvn_numb >= 3: # If it is true, then print 5 separated by spaces using the end function. print("5 ", end="") # Take another variable say m and initialize its value to 1. m = 1 # Take another variable say n and initialize its value to 2. n = 2 # Take another variable say o and initialize its value to 5. o = 5 # 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, add the above 3 values m, n, o and store them in a variable # say rslt. rslt = m + n + o # Assign the value of n to m. m = n # Assign the value of o to n. n = o # Assign the value of rslt to o. o = rslt # Print the value of the rslt separated by spaces using the end function. print(rslt, end=" ") # Increment the above itr value by 1. itr += 1
Output:
The above series till the given number{ 13 } is : 1 2 5 8 15 28 51 94 173 318 585 1076 1979
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 4.
- Check if the given number is greater than or equal to 1 using the if conditional statement.
- If it is true, then print 1 separated by spaces using the end function.
- Check if the given number is greater than or equal to 2 using the if conditional statement.
- If it is true, then print 2 separated by spaces using the end function.
- Check if the given number is greater than or equal to 3 using the if conditional statement.
- If it is true, then print 5 separated by spaces using the end function.
- Take another variable say m and initialize its value to 1.
- Take another variable say n and initialize its value to 2.
- Take another variable say o and initialize its value to 5.
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Inside the loop, add the above 3 values m, n, o and store them in a variable say rslt.
- Assign the value of n to m.
- Assign the value of o to n.
- Assign the value of rslt to o.
- Print the value of the rslt 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 4. itr = 4 print("The above series till the given number{", gvn_numb, "} is :") # Check if the given number is greater than or equal to 1 using the if conditional # statement. if gvn_numb >= 1: # If it is true, then print 1 separated by spaces using the end function. print("1 ", end="") # Check if the given number is greater than or equal to 2 using the if conditional # statement. if gvn_numb >= 2: # If it is true, then print 2 separated by spaces using the end function. print("2 ", end="") # Check if the given number is greater than or equal to 3 using the if conditional # statement. if gvn_numb >= 3: # If it is true, then print 5 separated by spaces using the end function. print("5 ", end="") # Take another variable say m and initialize its value to 1. m = 1 # Take another variable say n and initialize its value to 2. n = 2 # Take another variable say o and initialize its value to 5. o = 5 # 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, add the above 3 values m, n, o and store them in a variable # say rslt. rslt = m + n + o # Assign the value of n to m. m = n # Assign the value of o to n. n = o # Assign the value of rslt to o. o = rslt # Print the value of the rslt separated by spaces using the end function. print(rslt, end=" ") # Increment the above itr value by 1. itr += 1
Output:
Enter some Random Number = 8 The above series till the given number{ 8 } is : 1 2 5 8 15 28 51 94
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.
Analyze yourself:
- How To Print Series In Python
- Number Series Programs In Python
- Python Series Programs
- Print 1 To 5 In Python
- Print Series In C
- In Python 3 What Is The Output Of 1/2
Related Posts On: