In the previous article, we have discussed Python Program To Print Cube Number Series 1 8 27 64…N
Given a number N and the task is to print the series (-1 4 -7 10 -13 16 -19…N) till the given number N in Python.
Examples:
Example1:
Input:
Given Number (Limit) = 12
Output:
The above series till the given number{ 12 } is :
-1 4 -7 10 -13 16 -19 22 -25 28 -31 34
Example2:
Input:
Given Number (Limit) = 15
Output:
The above series till the given number{ 15 } is :
-1 4 -7 10 -13 16 -19 22 -25 28 -31 34 -37 40 -43
- Python Program to Print Series 6, 9, 14, 21, 30, 41, 54 … N
- Python Program to Print Series 0 2 6 12 20 30 42…N
- Python Program to Find Sum of Series 1/2!+2/3!+3/5!+…..N/(N+1)!
Program to Print Series -1 4 -7 10 -13 16 -19…n in Python
Below are the ways to print the series (-1 4 -7 10 -13 16 -19…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 p 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.
- Inside the loop, check if the above variable itr value is even or not using the if conditional statement.
- If it is true, then print the value of p separated by spaces.
- Else, print the value of -1 multiplied with p separated by spaces.
- Increment the above p value by 3.
- 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 = 12
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say p and initialize its value to 1.
p = 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):
#Inside the loop, check if the above variable itr value is even or not using the
#if conditional statement.
if(itr % 2 == 0):
#If it is true, then print the value of p separated by spaces.
print(p, end=" ")
else:
#Else, print the value of -1 multiplied with p separated by spaces.
print(-1*p, end=" ")
#Increment the above p value by 3.
p += 3
#Increment the above itr value by 1.
itr += 1
Output:
The above series till the given number{ 12 } is :
-1 4 -7 10 -13 16 -19 22 -25 28 -31 34
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 p 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.
- Inside the loop, check if the above variable itr value is even or not using the if conditional statement.
- If it is true, then print the value of p separated by spaces.
- Else, print the value of -1 multiplied with p separated by spaces.
- Increment the above p value by 3.
- 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 p and initialize its value to 1.
p = 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):
#Inside the loop, check if the above variable itr value is even or not using the
#if conditional statement.
if(itr % 2 == 0):
#If it is true, then print the value of p separated by spaces.
print(p, end=" ")
else:
#Else, print the value of -1 multiplied with p separated by spaces.
print(-1*p, end=" ")
#Increment the above p value by 3.
p += 3
#Increment the above itr value by 1.
itr += 1
Output:
Enter some Random Number = 15
The above series till the given number{ 15 } is :
-1 4 -7 10 -13 16 -19 22 -25 28 -31 34 -37 40 -43
Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.
Also Read:
- Write A Program To Print The Following Series 1 4 7 10 In Python
- Write A Program To Print The Following Series 1 4 7 10
- 13//4 In Python
- 7^10 In Python
- N! In Python
Related Posts On: