Program to Print Series 1, -2, 6, -15, 31 ... N

Python Program to Print Series 1, -2, 6, -15, 31 … N

In the previous article, we have discussed Python Program to Print Series 1, 3, 7, 15, 31 … N
Given a number N and the task is to print the series (1, -2, 6, -15, 31 … N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number (Limit) = 8

Output:

The above series till the given number{ 8 } is :
1 -2 6 -15 31 -56 92 -141

Example2:

Input:

Given Number (Limit) = 11

Output:

The above series till the given number{ 11 } is :
1 -2 6 -15 31 -56 92 -141 205 -286 386

Program to Print Series 1, -2, 6, -15, 31 … N in Python

Below are the ways to print the series (1, -2, 6, -15, 31 … 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 previous_val 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 itr value is even or not using the if conditional statement.
  • If it is true, then print the value of above previous_val multiplied with -1 separated by spaces.
  • Else, Print the value of the above previous_val separated by spaces.
  • Calculate the value of itr raised to power 2 using the pow() function and add it to the previous_val.
  • Store it in the same variable previous_val.
  • 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 = 8
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say previous_val and initialize its value to 1.
previous_val = 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 itr value is even or not using the if
   # conditional statement.
    if(itr % 2 == 0):
         # If it is true, then print the value of above previous_val multiplied with -1
         # separated by spaces.
        print(-1*previous_val, end=" ")
    else:
        # Else, Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
   # Calculate the value of itr raised to power 2 using the pow() function and
   # add it to the previous_val.
   # Store it in the same variable previous_val.
    previous_val += pow(itr, 2)
    # Increment the above itr value by 1.
    itr += 1

Output:

The above series till the given number{ 8 } is :
1 -2 6 -15 31 -56 92 -141

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 previous_val 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 itr value is even or not using the if conditional statement.
  • If it is true, then print the value of above previous_val multiplied with -1 separated by spaces.
  • Else, Print the value of the above previous_val separated by spaces.
  • Calculate the value of itr raised to power 2 using the pow() function and add it to the previous_val.
  • Store it in the same variable previous_val.
  • 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 previous_val and initialize its value to 1.
previous_val = 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 itr value is even or not using the if
   # conditional statement.
    if(itr % 2 == 0):
         # If it is true, then print the value of above previous_val multiplied with -1
         # separated by spaces.
        print(-1*previous_val, end=" ")
    else:
        # Else, Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
   # Calculate the value of itr raised to power 2 using the pow() function and
   # add it to the previous_val.
   # Store it in the same variable previous_val.
    previous_val += pow(itr, 2)
    # Increment the above itr value by 1.
    itr += 1

Output:

Enter some Random Number = 11
The above series till the given number{ 11 } is :
1 -2 6 -15 31 -56 92 -141 205 -286 386

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.