Python

Program to Print Series -1 4 -7 10 -13 16 -19...n

Python Program to Print Series -1 4 -7 10 -13 16 -19…n

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.

Python Program to Print Series -1 4 -7 10 -13 16 -19…n Read More »

Program To Print Cube Number Series 1 8 27 64...N

Python Program To Print Cube Number Series 1 8 27 64…N

In the previous article, we have discussed Python Program to Print Series 6, 9, 14, 21, 30, 41, 54 … N
Given a number N and the task is to print the cube number series (1 8 27 64…N) till the given number N in Python.

Cube number:

A cube number is a number that has been multiplied by itself three times. This is also known as ‘a number cubed.’ Cubed is represented by the symbol ³.

For example :

The cube number of 3= 3³ =3*3*3 = 27.

Examples:

Example1:

Input:

Given Number (Limit) = 8

Output:

The above cube series till the given number{ 8 } is :
1 8 27 64 125 216 343 512

Example2:

Input:

Given Number (Limit) = 6

Output:

The above cube series till the given number{ 6 } is :
1 8 27 64 125 216

Program To Print Cube Number Series 1 8 27 64…N in Python

Below are the ways to print the cube number series (1 8 27 64…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.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the while loop, multiply the itr with itself 3 times and store it in another variable.
  • Print the above result separated by spaces.
  • 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
print("The above cube 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 while loop, multiply the itr with itself 3 times and store it in
    # another variable.
    k = itr*itr*itr
    # Print the above result separated by spaces.
    print(k, end=" ")
    # Increment the above itr value by 1.
    itr += 1

Output:

The above cube series till the given number{ 8 } is :
1 8 27 64 125 216 343 512

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.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the while loop, multiply the itr with itself 3 times and store it in another variable.
  • Print the above result separated by spaces.
  • 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
print("The above cube 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 while loop, multiply the itr with itself 3 times and store it in
    # another variable.
    k = itr*itr*itr
    # Print the above result separated by spaces.
    print(k, end=" ")
    # Increment the above itr value by 1.
    itr += 1

Output:

Enter some Random Number = 6
The above cube series till the given number{ 6 } is :
1 8 27 64 125 216

The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners. Using them you can develop code on your own and master coding skills.

 

Python Program To Print Cube Number Series 1 8 27 64…N Read More »

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.

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

Program to Print Series 1, 3, 7, 15, 31 ... N

Python Program to Print Series 1, 3, 7, 15, 31 … N

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

Examples:

Example1:

Input:

Given Number (Limit) = 5

Output:

The above series till the given number{ 5 } is :
1 3 7 15 31

Example2:

Input:

Given Number (Limit) = 9

Output:

The above series till the given number{ 9 } is :
1 3 7 15 31 63 127 255 511

Program to Print Series 1, 3, 7, 15, 31 … N in Python

Below are the ways to print the series (1, 3, 7, 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 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, Multiply the variable previous_val with 2 and add 1 to the result.
  • 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 = 5
# 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
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, Multiply the variable previous_val with 2 and add 1 to the result.
        # Store it in the same variable previous_val.
    previous_val = (previous_val * 2) + 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{ 5 } is :
1 3 7 15 31

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 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, Multiply the variable previous_val with 2 and add 1 to the result.
  • 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 (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 0.
previous_val = 0
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, Multiply the variable previous_val with 2 and add 1 to the result.
        # Store it in the same variable previous_val.
    previous_val = (previous_val * 2) + 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 = 9
The above series till the given number{ 9 } is :
1 3 7 15 31 63 127 255 511

If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.

Python Program to Print Series 1, 3, 7, 15, 31 … N Read More »

Program to Print Series 2, 4, 7, 2, 21, ... N

Python Program to Print Series 2, 4, 7, 12, 21, … N

In the previous article, we have discussed Python Program to Print Series 2 ,15, 41, 80…n
Given a number N and the task is to print the series (2, 4, 7, 12, 21, … 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 :
2 4 7 12 21 38 71 136

Example2:

Input:

Given Number (Limit) = 10

Output:

The above series till the given number{ 10 } is :
2 4 7 12 21 38 71 136 265 522

Program to Print Series 2 4 7 12 21 … N in Python

Below are the ways to print the series (2, 4, 7, 12, 21, … 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 0.
  • Take another variable say previous_val and initialize its value to 2.
  • Print the value 2 separated by spaces.
  • Loop until the above-declared variable itr value is less than the given number-1 using the while loop.
  • Inside the loop, Multiply the variable previous_val with 2 and subtract the above itr value from it.
  • 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(limit) as static input and store it in a variable.
gvn_numb = 8
# Take a variable to say itr and initialize its value to 0.
itr = 0
# Take another variable say previous_val and initialize its value to 2.
previous_val = 2
print("The above series till the given number{", gvn_numb, "} is :")
# print the value 2 separated by spaces.
print("2 ",end="")
# Loop until the above-declared variable itr value is less than the
# given number-1 using the while loop.
while itr<gvn_numb-1:
    #Inside the loop, Multiply the variable previous_val with 2 and subtract the above
    #itr value from it.
    #Store it in the same variable previous_val.
    previous_val = (previous_val * 2) -itr
    # 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 :
2 4 7 12 21 38 71 136

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 0.
  • Take another variable say previous_val and initialize its value to 2.
  • Print the value 2 separated by spaces.
  • Loop until the above-declared variable itr value is less than the given number-1 using the while loop.
  • Inside the loop, Multiply the variable previous_val with 2 and subtract the above itr value from it.
  • 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 (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 0.
itr = 0
# Take another variable say previous_val and initialize its value to 2.
previous_val = 2
print("The above series till the given number{", gvn_numb, "} is :")
# print the value 2 separated by spaces.
print("2 ",end="")
# Loop until the above-declared variable itr value is less than the
# given number-1 using the while loop.
while itr<gvn_numb-1:
    #Inside the loop, Multiply the variable previous_val with 2 and subtract the above
    #itr value from it.
    #Store it in the same variable previous_val.
    previous_val = (previous_val * 2) -itr
    # 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 = 10
The above series till the given number{ 10 } is :
2 4 7 12 21 38 71 136 265 522

Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.

Python Program to Print Series 2, 4, 7, 12, 21, … N Read More »

Program to Print Series 2 ,15, 41, 80...n

Python Program to Print Series 2 ,15, 41, 80…n

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

Examples:

Example1:

Input:

Given Number(limit) = 15

Output:

The above series till the given number{ 15 } is :
2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367

Example2:

Input:

Given Number(limit) = 20

Output:

The above series till the given number{ 20 } is :
2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367 1562 1770 1991 2225 2472

Program to Print Series 2,15,41,80…n in Python

Below are the ways to print the series ( 2,15,41,80…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 valu and initialize its value to 2.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the loop, print the value of the above variable valu separated by spaces.
  • Multiply the value of itr with 13 and add the result to the valu.
  • Store the above result in the same variable valu.
  • 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 = 15
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say valu and initialize its value to 2.
valu=2
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, print the value of the above variable valu 
    #separated by spaces.
    print(valu,end=" ")
    #Multiply the value of itr with 13 and add the result to the valu.
    #Store the above result in the same variable valu.
    valu+=itr*13
    #Increment the above itr value by 1.
    itr+=1

Output:

The above series till the given number{ 15 } is :
2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367

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 valu and initialize its value to 2.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the loop, print the value of the above variable valu separated by spaces.
  • Multiply the value of itr with 13 and add the result to the valu.
  • Store the above result in the same variable valu.
  • 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 valu and initialize its value to 2.
valu=2
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, print the value of the above variable valu 
    #separated by spaces.
    print(valu,end=" ")
    #Multiply the value of itr with 13 and add the result to the valu.
    #Store the above result in the same variable valu.
    valu+=itr*13
    #Increment the above itr value by 1.
    itr+=1

Output:

Enter some Random Number = 20
The above series till the given number{ 20 } is :
2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367 1562 1770 1991 2225 2472

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.

Python Program to Print Series 2 ,15, 41, 80…n Read More »

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.

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

Program to Print Series 0,2,8,14,24,34 ...N

Python Program to Print Series 0, 2, 8, 14, 24, 34 …N

In the previous article, we have discussed Python Program to Print Series 1, 22, 333, 4444…n
Given a number N and the task is to print the series ( 0,2,8,14,24,34 …N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number = 5

Output:

The above series till the given number{ 5 } is :
0 2 8 14 24

Example2:

Input:

Given Number = 9

Output:

The above series till the given number{ 9 } is :
0 2 8 14 24 34 48 62 80

Program to Print Series 0,2,8,14,24,34 …N in Python

Below are the ways to print the series ( 0,2,8,14,24,34 …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 itr raised to the power 2 using the pow() function and subtract 2 from it.
  • Store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Else, If it is false calculate the value of itr raised to the power 2 using the pow() function and subtract 1 from it.
  • 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 = 5
# 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
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, calculate the value of itr raised to the power 2 using the pow()
          # function and subtract 2 from it.
      # Store it in the same variable previous_val.
        previous_val = pow(itr, 2) - 2
        # Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
    else:
     # Else, If it is false calculate the value of itr raised to the power 2 using the pow()
         # function and subtract 1 from it.
         # Store it in the same variable previous_val.
        previous_val = pow(itr, 2) - 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{ 5 } is :
0 2 8 14 24

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 itr raised to the power 2 using the pow() function and subtract 2 from it.
  • Store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Else, If it is false calculate the value of itr raised to the power 2 using the pow() function and subtract 1 from it.
  • 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
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, calculate the value of itr raised to the power 2 using the pow()
          # function and subtract 2 from it.
      # Store it in the same variable previous_val.
        previous_val = pow(itr, 2) - 2
        # Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
    else:
     # Else, If it is false calculate the value of itr raised to the power 2 using the pow()
         # function and subtract 1 from it.
         # Store it in the same variable previous_val.
        previous_val = pow(itr, 2) - 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 = 9
The above series till the given number{ 9 } is :
0 2 8 14 24 34 48 62 80

Grab the opportunity and utilize the Python Program Code Examples over here to prepare basic and advanced topics too with ease and clear all your doubts.

Python Program to Print Series 0, 2, 8, 14, 24, 34 …N Read More »

Program to Print Series 1,22,333,4444...n

Python Program to Print Series 1, 22, 333, 4444…n

In the previous article, we have discussed Python Program to Print Series 6, 11, 21, 36, 56…n
Given a number N and the task is to print the series (1,22,333,4444…N) for the given number N in Python.

Examples:

Example1:

Input:

Given Number = 5

Output:

The above series till the given number{ 5 } is :
 1 22 333 4444 55555

Example2:

Input:

Given Number = 8

Output:

The above series till the given number{ 8 } is :
 1 22 333 4444 55555 666666 7777777 88888888

Program to Print Series 1,22,333,4444…n in Python

Below are the ways to print the series (1,22,333,4444…N) for the given number N in Python.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Loop till the given number using the for loop.
  • Inside the For loop, Iterate till the above iterator value of the parent for loop using another Nested For loop(Inner For loop).
  • Print the iterator value of the parent for loop separated by spaces.
  • Print the space by using the end keyword.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 5
print("The above series till the given number{", gvn_numb, "} is :")
# Loop till the given number using the for loop.
for itr in range(gvn_numb+1):
  # Inside the For loop, Iterate till the above iterator value of the parent for loop
  # using another Nested For loop(Inner For loop).
    for m in range(itr):
      # Print the iterator value of the parent for loop separated by spaces.
        print(itr, end="")
    # Print the space by using the end keyword.
    print(end=" ")

Output:

The above series till the given number{ 5 } is :
 1 22 333 4444 55555

Method #2: Using For loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Loop till the given number using the for loop.
  • Inside the For loop, Iterate till the above iterator value of the parent for loop using another Nested For loop(Inner For loop).
  • Print the iterator value of the parent for loop separated by spaces.
  • Print the space by using the end keyword.
  • 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 = "))
print("The above series till the given number{", gvn_numb, "} is :")
# Loop till the given number using the for loop.
for itr in range(gvn_numb+1):
  # Inside the For loop, Iterate till the above iterator value of the parent for loop
  # using another Nested For loop(Inner For loop).
    for m in range(itr):
      # Print the iterator value of the parent for loop separated by spaces.
        print(itr, end="")
    # Print the space by using the end keyword.
    print(end=" ")

Output:

Enter some Random Number = 8
The above series till the given number{ 8 } is :
1 22 333 4444 55555 666666 7777777 88888888

Practice Python Program Examples to master coding skills and learn the fundamental concepts in the dynamic programming language Python.

Python Program to Print Series 1, 22, 333, 4444…n Read More »

Program to Print Series 6,11,21,36,56...n

Python Program to Print Series 6, 11, 21, 36, 56…n

In the previous article, we have discussed Python Program to Print Series 1, 2, 4, 8, 16, 32…n
Given a number N and the task is to print the series (6,11,21,36,56…N) for the given number N in Python.

Examples:

Example1:

Input:

Given Number = 6

Output:

The above series till the given number{ 6 } is :
6 11 21 36 56 81

Example2:

Input:

Given Number = 10

Output:

The above series till the given number{ 10 } is :
6 11 21 36 56 81 111 146 186 231

Program to Print Series 6,11,21,36,56…n in Python

Below are the ways to print the series (6,11,21,36,56…N) for 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 to say previos_val and initialize its value to 6.
  • Take the other variable to say gvn_diffence and initialize its value to 5 (which is the common difference).
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the while loop, print the value of the above previos_val value separated by spaces.
  • Add the given difference to the previous value and store it in the same variable previos_val.
  • Add 5 to the given difference and store it in the same variable.
  • Increment the value of the above itr value by 1 and store it in the same variable.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 6
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable to say previos_val and initialize its value to 6.
previos_val = 6
# Take the other variable to say gvn_diffence and initialize its value to 5
# (which is the common difference).
gvn_diffence = 5
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 while loop, print the value of the above previos_val value
        # separated by spaces.
    print(previos_val, end=" ")
    # Add the given difference to the previous value and store it in the same
    # variable previos_val.
    previos_val = previos_val + gvn_diffence
    # Add 5 to the given difference and store it in the same variable.
    gvn_diffence = gvn_diffence + 5
    # Increment the value of the above itr value by 1 and store it in the
    # same variable.
    itr += 1

Output:

The above series till the given number{ 6 } is :
6 11 21 36 56 81

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 to say previos_val and initialize its value to 6.
  • Take the other variable to say gvn_diffence and initialize its value to 5 (which is the common difference).
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the while loop, print the value of the above previos_val value separated by spaces.
  • Add the given difference to the previous value and store it in the same variable previos_val.
  • Add 5 to the given difference and store it in the same variable.
  • Increment the value of the above itr value by 1 and store it in the same variable.
  • 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 to say previos_val and initialize its value to 6.
previos_val = 6
# Take the other variable to say gvn_diffence and initialize its value to 5
# (which is the common difference).
gvn_diffence = 5
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 while loop, print the value of the above previos_val value
        # separated by spaces.
    print(previos_val, end=" ")
    # Add the given difference to the previous value and store it in the same
    # variable previos_val.
    previos_val = previos_val + gvn_diffence
    # Add 5 to the given difference and store it in the same variable.
    gvn_diffence = gvn_diffence + 5
    # Increment the value of the above itr value by 1 and store it in the
    # same variable.
    itr += 1

Output:

Enter some Random Number = 10
The above series till the given number{ 10 } is :
6 11 21 36 56 81 111 146 186 231

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.

Python Program to Print Series 6, 11, 21, 36, 56…n Read More »