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
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.