Program to Print Series 1 9 17 33 49 73 97 ...N

Python Program to Print Series 1 9 17 33 49 73 97 …N

In the previous article, we have discussed Python Program to Print Series 0, 2, 8, 14, 24, 34 …N
Given a number N and the task is to print the series (1 9 17 33 49 73 97 …N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number = 8

Output:

The above series till the given number{ 8 } is :
1 9 17 33 49 73 97 129

Example2:

Input:

Given Number = 4

Output:

The above series till the given number{ 4 } is :
1 9 17 33

Program to Print Series 1 9 17 33 49 73 97 …N in Python

Below are the ways to print the series (1 9 17 33 49 73 97 …N) till the given number N in Python:

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N 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 0.
  • 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, calculate the value of 2 multiplied with the above itr value raised to the power 2 using the pow() function.
  • Store it in a variable.
  • Add 1 to the above result and store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • If it is false, calculate the value of 2 multiplied with the above itr value raised to the power 2 using the pow() function.
  • Store it in another variable.
  • Subtract 1 from the above result and store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N 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 0.
previous_val = 0
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
print("The above series till the given number{", gvn_numb, "} is :")
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, calculate the value of 2 multiplied with the above itr value raised
        # to the power 2 using the pow() function. 
        # Store it in a variable.
        k = 2*pow(itr, 2)
        # Add 1 to the above result and store it in the same variable previous_val.
        previous_val = k+1
        # Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
    else:
       # If it is false, calculate the value of 2 multiplied with the above itr value raised
       # to the power 2 using the pow() function. 
       # Store it in another variable.
        l = 2*pow(itr, 2)
        # Subtract 1 from the above result and store it in the same variable previous_val.
        previous_val = l - 1
        # Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
  # Increment the above itr value by 1.
    itr += 1

Output:

The above series till the given number{ 8 } is :
1 9 17 33 49 73 97 129

Method #2: Using While loop (User Input)

Approach:

  • Give the number N 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 0.
  • 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, calculate the value of 2 multiplied with the above itr value raised to the power 2 using the pow() function.
  • Store it in a variable.
  • Add 1 to the above result and store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • If it is false, calculate the value of 2 multiplied with the above itr value raised to the power 2 using the pow() function.
  • Store it in another variable.
  • Subtract 1 from the above result and store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N 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 0.
previous_val = 0
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
print("The above series till the given number{", gvn_numb, "} is :")
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, calculate the value of 2 multiplied with the above itr value raised
        # to the power 2 using the pow() function. 
        # Store it in a variable.
        k = 2*pow(itr, 2)
        # Add 1 to the above result and store it in the same variable previous_val.
        previous_val = k+1
        # Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
    else:
       # If it is false, calculate the value of 2 multiplied with the above itr value raised
       # to the power 2 using the pow() function. 
       # Store it in another variable.
        l = 2*pow(itr, 2)
        # Subtract 1 from the above result and store it in the same variable previous_val.
        previous_val = l - 1
        # Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
  # Increment the above itr value by 1.
    itr += 1

Output:

Enter some Random Number = 4
The above series till the given number{ 4 } is :
1 9 17 33

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.