Python Program to Print Even Length Words in a String

Program to Print Even Length Words in a String

Given multiple strings separated by spaces, the task is to print even-length words in the given string in Python.

Examples:

Example1:

Input:

Given String ='Hello Good morning this is BTechgeeks online programming platform'

Output:

Even length words in the given string [ Hello Good morning this is BTechgeeks online programming platform ] are:
Good
this
is
BTechgeeks
online
platform

Example2:

Input:

Given String='hello this is btechgeeks online coding platform for btech students'

Output:

Even length words in the given string [ hello this is btechgeeks online coding platform for btech students ] are:
this
is
btechgeeks
online
coding
platform
students

Program to Print Even Length Words in a String in Python

Below are the ways to print even-length words in the given string in Python.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Convert this string into a list of words and split them with spaces using split() and list() functions.
  • Loop in the words list using For loop.
  • Calculate the length of the word using the len() function.
  • Check if the length of the word is even or not using the If conditional Statement.
  • If it is true then print the word.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvnstrng = 'Hello Good morning this is BTechgeeks online programming platform'
print('Even length words in the given string [', gvnstrng, '] are:')
# Convert this string into a list of words and
# split them with spaces using split() and list() functions.
strngwordslist = list(gvnstrng.split())
# Loop in the words list using For loop.
for strngword in strngwordslist:
        # Calculate the length of the word using the len() function.
    wordleng = len(strngword)
    # Check if the length of the word is even or not
    # using the If conditional Statement.
    if(wordleng % 2 == 0):
        # If it is true then print the word.
        print(strngword)

Output:

Even length words in the given string [ Hello Good morning this is BTechgeeks online programming platform ] are:
Good
this
is
BTechgeeks
online
platform

Method #2: Using For Loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Convert this string into a list of words and split them with spaces using split() and list() functions.
  • Loop in the words list using For loop.
  • Calculate the length of the word using the len() function.
  • Check if the length of the word is even or not using the If conditional Statement.
  • If it is true then print the word.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvnstrng = input('Enter some random string = ')
print('Even length words in the given string [', gvnstrng, '] are:')
# Convert this string into a list of words and
# split them with spaces using split() and list() functions.
strngwordslist = list(gvnstrng.split())
# Loop in the words list using For loop.
for strngword in strngwordslist:
        # Calculate the length of the word using the len() function.
    wordleng = len(strngword)
    # Check if the length of the word is even or not
    # using the If conditional Statement.
    if(wordleng % 2 == 0):
        # If it is true then print the word.
        print(strngword)

Output:

Enter some random string = hello this is btechgeeks online coding platform for btech students
Even length words in the given string [ hello this is btechgeeks online coding platform for btech students ] are:
this
is
btechgeeks
online
coding
platform
students

Related Programs:

Python Program to Print Series 0 2 6 12 20 30 42…N

Program to Print Series 0 2 6 12 20 30 42...N

In the previous article, we have discussed Python Program to Print Series 1 2 5 8 15 28 51 94….N
Given a number N and the task is to print the series (0 2 6 12 20 30 42…N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number (Limit) = 9

Output:

The above series till the given number{ 9 } is :
0 2 6 12 20 30 42 56 72

Example2:

Input:

Given Number (Limit) = 10

Output:

The above series till the given number{ 10 } is :
0 2 6 12 20 30 42 56 72 90

Program to Print Series 0 2 6 12 20 30 42…N in Python

Below are the ways to print the series (0 2 6 12 20 30 42…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.
  • Multiply the above itr value with itself and subtract again the itr value from it.
  • Store it in another variable.
  • Print the value of the above result separated by spaces using the end function.
  • 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 = 9
# Take a variable to say itr and initialize its value to 1.
itr = 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:
    # Multiply the above itr value with itself and subtract again the itr value from it.
    # Store it in another variable.
    k = (itr*itr)-itr
    # Print the value of the above result separated by spaces using the end function.
    print(k, end=" ")
    # Increment the above itr value by 1.
    itr += 1

Output:

The above series till the given number{ 9 } is :
0 2 6 12 20 30 42 56 72

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.
  • Multiply the above itr value with itself and subtract again the itr value from it.
  • Store it in another variable.
  • Print the value of the above result separated by spaces using the end function.
  • 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 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:
    # Multiply the above itr value with itself and subtract again the itr value from it.
    # Store it in another variable.
    k = (itr*itr)-itr
    # Print the value of the above result separated by spaces using the end function.
    print(k, 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 :
0 2 6 12 20 30 42 56 72 90

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

Python Program to Print Series 0, 6, 10, 17, 22, 30, 36…N

Program to Print Series 0, 6, 10, 17, 22, 30, 36...N

In the previous article, we have discussed Python Program to Print Series 10, 5, 60, 15, 110 …N
Given a number N and the task is to print the series (0, 6, 10, 17, 22, 30, 36…N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number (Limit) = 16

Output:

The above series till the given number{ 16 } is :
0 6 10 17 22 30 36 45 52 62 70 81 90 102 112 125

Example2:

Input:

Given Number (Limit) = 11

Output:

The above series till the given number{ 11 } is :
0 6 10 17 22 30 36 45 52 62 70

Program to Print Series 0, 6, 10, 17, 22, 30, 36…N in Python

Below are the ways to print the series (0, 6, 10, 17, 22, 30, 36…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 m and initialize its value to 0.
  • Take another variable say n and initialize its value to 6.
  • Take another variable say x and initialize its value to 10.
  • Take another variable say y and initialize its value to 11.
  • 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 separated by spaces.
  • Add the above y value to n and store it in the same variable n.
  • Increment the above y value by 2.
  • Else,  print the value of separated by spaces.
  • Add the above x value to m and store it in the same variable m.
  • Increment the above x value by 2.
  • 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 = 16
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say m and initialize its value to 0.
m = 0
# Take another variable say n and initialize its value to 6.
n = 6
# Take another variable say x and initialize its value to 10.
x = 10
# Take another variable say y and initialize its value to 11.
y = 11
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 n separated by spaces.
        print(n, end=" ")
        # Add the above y value to n and store it in the same variable n.
        n += y
        # Increment the above y value by 2.
        y += 2
    else:
        # Else, print the value of m separated by spaces.
        print(m, end=" ")
        # Add the above x value to m and store it in the same variable m.
        m += x
        # Increment the above x value by 2.
        x += 2
   # Increment the above itr value by 1.
    itr += 1

Output:

The above series till the given number{ 16 } is :
0 6 10 17 22 30 36 45 52 62 70 81 90 102 112 125

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 m and initialize its value to 0.
  • Take another variable say n and initialize its value to 6.
  • Take another variable say x and initialize its value to 10.
  • Take another variable say y and initialize its value to 11.
  • 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 separated by spaces.
  • Add the above y value to n and store it in the same variable n.
  • Increment the above y value by 2.
  • Else,  print the value of separated by spaces.
  • Add the above x value to m and store it in the same variable m.
  • Increment the above x value by 2.
  • 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 m and initialize its value to 0.
m = 0
# Take another variable say n and initialize its value to 6.
n = 6
# Take another variable say x and initialize its value to 10.
x = 10
# Take another variable say y and initialize its value to 11.
y = 11
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 n separated by spaces.
        print(n, end=" ")
        # Add the above y value to n and store it in the same variable n.
        n += y
        # Increment the above y value by 2.
        y += 2
    else:
        # Else, print the value of m separated by spaces.
        print(m, end=" ")
        # Add the above x value to m and store it in the same variable m.
        m += x
        # Increment the above x value by 2.
        x += 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 :
0 6 10 17 22 30 36 45 52 62 70

 

Python Program to Print Series 10, 5, 60, 15, 110 …N

Program to Print Series 10, 5, 60, 15, 110 ...N

In the previous article, we have discussed Python Program to Print Series -1 4 -7 10 -13 16 -19…n
Given a number N and the task is to print the series ( 10, 5, 60, 15, 110 …N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number (Limit) = 14

Output:

The above series till the given number{ 14 } is :
10 5 60 15 110 25 160 35 210 45 260 55 310 65

Example2:

Input:

Given Number (Limit) = 9

Output:

The above series till the given number{ 9 } is :
10 5 60 15 110 25 160 35 210

Program to Print Series 10, 5, 60, 15, 110 …N in Python

Below are the ways to print the series ( 10, 5, 60, 15, 110 …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 10.
  • Take another variable say q and initialize its value to 5.
  • 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 separated by spaces.
  • Increment the above q value by 10 and store it in the same variable.
  • Else, print the value of separated by spaces.
  • Increment the above p value by 50 and store it in the same variable.
  • 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 = 14
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say p and initialize its value to 10.
p = 10
# Take another variable say q and initialize its value to 5.
q = 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 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 q separated by spaces.
        print(q, end=" ")
        # Increment the above q value by 10 and store it in the same variable.
        q += 10
    else:
        # Else, print the value of p separated by spaces.
        print(p, end=" ")
        # Increment the above p value by 50 and store it in the same variable.
        p += 50
    # Increment the above itr value by 1.
    itr += 1

Output:

The above series till the given number{ 14 } is :
10 5 60 15 110 25 160 35 210 45 260 55 310 65

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 10.
  • Take another variable say q and initialize its value to 5.
  • 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 separated by spaces.
  • Increment the above q value by 10 and store it in the same variable.
  • Else, print the value of separated by spaces.
  • Increment the above p value by 50 and store it in the same variable.
  • 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 10.
p = 10
# Take another variable say q and initialize its value to 5.
q = 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 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 q separated by spaces.
        print(q, end=" ")
        # Increment the above q value by 10 and store it in the same variable.
        q += 10
    else:
        # Else, print the value of p separated by spaces.
        print(p, end=" ")
        # Increment the above p value by 50 and store it in the same variable.
        p += 50
    # Increment the above itr value by 1.
    itr += 1

Output:

Enter some Random Number = 9
The above series till the given number{ 9 } is :
10 5 60 15 110 25 160 35 210

Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.

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

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 2, 4, 7, 12, 21, … N

Program to Print Series 2, 4, 7, 2, 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 ,15, 41, 80…n

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 Check If a Line Touches or Intersects a Circle

Program to Check If a Line Touches or Intersects a Circle

In the previous article, we have discussed Python Program for Pizza Cut Problem (Or Circle Division by Lines)
Given the coordinates of the center point as (0,0), the radius of the circle, and the equation of a line and the task is to check whether the given line touches or intersects the circle.

The line equation is in the form ax+by+c.

Hence a, b, c values are given for a line equation.

The three possibilities :

  • The Line intersect the circle
  • The Line touches the circle
  • The Line outside the circle

The following formula can be used to calculate the distance of a line from a point:

(ax+by+c)/sqrt(a*a+b*b)

If d > r,  the line lies outside the circle.
If d = r,  the line touches the circle.
If d < r,  the line intersects the circle.

where d = the distance of a line from a center.

r is the radius of the circle.

Examples:

Example1:

Input:

Given radius = 6
Given  a = 2
Given  b = 1
Given  c = 1

Output:

The Given line intersects the circle

Example2:

Input:

Given radius = 5
Given a = 1
Given b = 1
Given c = -15

Output:

The Given line is outside the circle

Program to Check If a Line Touches or Intersects a Circle in Python

Below are the ways to check whether the given line touches or intersects the circle in Python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the radius as static input and store it in a variable.
  • Take the x-coordinate of the center and initialize its value to 0.
  • Take the y-coordinate of the center and initialize its value to 0.
  • Give the number as static input and store it in a variable.
  • Give the other number as static input and store it in another variable.
  • Give the third number as static input and store it in another variable.
  • Calculate the distance of the given line from the given center point using the above given mathematical formula, abs(), math.sqrt() functions.
  • Store it in another variable.
  • Check if the given radius value is equal to the above-obtained distance using the if conditional statement.
  • If it is true, then print “The Given line touches the circle”.
  • Check if the given radius value is greater than the above-obtained distance using the elif conditional statement.
  • If it is true, then print “The Given line intersects the circle”.
  • Else, print “The Given line is outside the circle”.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the radius as static input and store it in a variable.
gvn_radiuss = 6
# Take the x-coordinate of center and initialize its value to 0.
m = 0
# Take the y-coordinate of center and initialize its value to 0.
n = 0
# Give the number as static input and store it in a variable.
p = 2
# Give the other number as static input and store it in another variable.
q = 1
# Give the third number as static input and store it in another variable.
r = 1
# Calculate the distance of the given line from the given center point using the
# above given mathematical formula, abs(), math.sqrt() functions
# store it in another variable.
distancee_val = ((abs(p * m + q * n + r)) /
                 math.sqrt(p * p + q * q))
# Check if the given radius value is equal to the above obtained distance using the
# if conditional statement.
if (gvn_radiuss == distancee_val):
    # If it is true, then print "The Given line touches the circle".
    print("The Given line touches the circle")
# Check if the given radius value is greater than the above obtained distance using
# the elif conditional statement.
elif (gvn_radiuss > distancee_val):
    # If it is true, then print "The Given line intersects the circle".
    print("The Given line intersects the circle")
else:
    # Else, print "The Given line is outside the circle".
    print("The Given line is outside the circle")

Output:

The Given line intersects the circle

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the radius as user input using the int(input()) function and store it in a variable.
  • Take the x-coordinate of the center and initialize its value to 0.
  • Take the y-coordinate of the center and initialize its value to 0.
  • Give the number as user input using the int(input()) function and store it in a variable.
  • Give the other number as user input using the int(input()) function and store it in another variable.
  • Give the third number as user input using the int(input()) function and store it in another variable.
  • Calculate the distance of the given line from the given center point using the above given mathematical formula, abs(), math.sqrt() functions.
  • Store it in another variable.
  • Check if the given radius value is equal to the above-obtained distance using the if conditional statement.
  • If it is true, then print “The Given line touches the circle”.
  • Check if the given radius value is greater than the above-obtained distance using the elif conditional statement.
  • If it is true, then print “The Given line intersects the circle”.
  • Else, print “The Given line is outside the circle”.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the radius as user input using the int(input()) function and 
# store it in a variable.
gvn_radiuss = int(input("Enter some random number = "))
# Take the x-coordinate of center and initialize its value to 0.
m = 0
# Take the y-coordinate of center and initialize its value to 0.
n = 0
# Give the number as user input using the int(input()) function and store it in a variable.
p = int(input("Enter some random number = "))
# Give the other number as user input using the int(input()) function and store it in another variable.
q = int(input("Enter some random number = "))
# Give the third number as user input using the int(input()) function and store it in another variable.
r = int(input("Enter some random number = "))
# Calculate the distance of the given line from the given center point using the
# above given mathematical formula, abs(), math.sqrt() functions
# store it in another variable.
distancee_val = ((abs(p * m + q * n + r)) /
                 math.sqrt(p * p + q * q))
# Check if the given radius value is equal to the above obtained distance using the
# if conditional statement.
if (gvn_radiuss == distancee_val):
    # If it is true, then print "The Given line touches the circle".
    print("The Given line touches the circle")
# Check if the given radius value is greater than the above obtained distance using
# the elif conditional statement.
elif (gvn_radiuss > distancee_val):
    # If it is true, then print "The Given line intersects the circle".
    print("The Given line intersects the circle")
else:
    # Else, print "The Given line is outside the circle".
    print("The Given line is outside the circle")

Output:

Enter some random number = 5
Enter some random number = 1
Enter some random number = 1
Enter some random number = -15
The Given line is outside the circle

Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.

Python Program for Pizza Cut Problem (Or Circle Division by Lines)

Program for Pizza Cut Problem (Or Circle Division by Lines)

In the previous article, we have discussed Python Program to Check Whether Triangle is Valid or Not if Sides are Given
Given the number of cuts and the task is to get the maximum number of pieces from the given number of cuts in python.

Formula:

1 + n*(n+1)/2

where n= number of cuts.

Examples:

Example1:

Input:

Given number of cuts = 4

Output:

The maximum number of pieces from the given number of cuts =  11

Example2:

Input:

Given number of cuts = 5

Output:

The maximum number of pieces from the given number of cuts =  16

Program for Pizza Cut Problem (Or Circle Division by Lines) in Python

Below are the ways to get the maximum number of pieces from the given number of cuts in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the number of cuts as static input and store it in a variable.
  • Create a function to say Get_Max_Pieces() which takes the given number of cuts as an argument, and returns the maximum number of pieces from the given number of cuts.
  • Inside the function, calculate the maximum number of pieces by using the above given mathematical formula and convert it to an integer using the int() function.
  • Store it in another variable.
  • Return the above result.
  • Pass the given number of cuts as an argument to the Get_Max_Pieces() function and store it in another variable.
  • Print the above result which is the maximum number of pieces from the given number of cuts.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say Get_Max_Pieces() which takes the given number of cuts as
# an argument, and returns the maximum number of pieces from the given number of cuts.


def Get_Max_Pieces(no_of_cuts):
    # Inside the function, calculate the maximum number of pieces by using the above
        # given mathematical formula and convert it to an integer using the int() function.
        # Store it in another variable.
    k = int(1 + no_of_cuts * (no_of_cuts + 1) / 2)
    # Return the above result.
    return k


# Give the number of cuts as static input and store it in a variable.
no_of_cuts = 4
# Pass the given number of cuts as an argument to the Get_Max_Pieces() function
# and store it in another variable.
maxim_pieces = Get_Max_Pieces(no_of_cuts)
# Print the above result which is the maximum number of pieces from the given
# number of cuts.
print("The maximum number of pieces from the given number of cuts = ", maxim_pieces)

Output:

The maximum number of pieces from the given number of cuts =  11

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the number of cuts as user input using the int(input()) function and store it in a variable.
  • Create a function to say Get_Max_Pieces() which takes the given number of cuts as an argument, and returns the maximum number of pieces from the given number of cuts.
  • Inside the function, calculate the maximum number of pieces by using the above given mathematical formula and convert it to an integer using the int() function.
  • Store it in another variable.
  • Return the above result.
  • Pass the given number of cuts as an argument to the Get_Max_Pieces() function and store it in another variable.
  • Print the above result which is the maximum number of pieces from the given number of cuts.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say Get_Max_Pieces() which takes the given number of cuts as
# an argument, and returns the maximum number of pieces from the given number of cuts.


def Get_Max_Pieces(no_of_cuts):
    # Inside the function, calculate the maximum number of pieces by using the above
        # given mathematical formula and convert it to an integer using the int() function.
        # Store it in another variable.
    k = int(1 + no_of_cuts * (no_of_cuts + 1) / 2)
    # Return the above result.
    return k


# Give the number of cuts as user input using the int(input()) function 
# and store it in a variable.
no_of_cuts = int(input("Enter some random number = "))
# Pass the given number of cuts as an argument to the Get_Max_Pieces() function
# and store it in another variable.
maxim_pieces = Get_Max_Pieces(no_of_cuts)
# Print the above result which is the maximum number of pieces from the given
# number of cuts.
print("The maximum number of pieces from the given number of cuts = ", maxim_pieces)

Output:

Enter some random number = 5
The maximum number of pieces from the given number of cuts = 16

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 Reverse Pyramid of Numbers

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Given the number of rows of the pyramid, the task is to print a Reverse Pyramid of Numbers in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows = 10

Output:

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1 
7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1

Example2:

Input:

Given number of rows = 6

Output:

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1

Program to Print Reverse Pyramid of Numbers in C,C++, and Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the parent loop iterator value to 0 in decreasing order using another For loop(Nested For Loop).
  • Print the iterator value of the inner for loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbrrows = 10
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbrrows):
    # Loop from the parent loop iterator value to 0 in decreasing order
    # using another For loop(Nested For Loop).
    for n in range(m, 0, -1):
        # Print the iterator value of the inner for loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1 
7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numbrrows = 10;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < numbrrows; m++) {
        // Loop from the parent loop iterator value to 0 in
        // decreasing order using another For loop(Nested
        // For Loop).
        for (int n = m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1 
7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numbrrows = 10;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < numbrrows; m++) {
        // Loop from the parent loop iterator value to 0 in
        // decreasing order using another For loop(Nested
        // For Loop).
        for (int n = m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1 
7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the parent loop iterator value to 0 in decreasing order using another For loop(Nested For Loop).
  • Print the iterator value of the inner for loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

Give the number of rows as user input using int(input()) and store it in a variable.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbrrows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbrrows):
    # Loop from the parent loop iterator value to 0 in decreasing order
    # using another For loop(Nested For Loop).
    for n in range(m, 0, -1):
        # Print the iterator value of the inner for loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

Enter some random number of rows = 6
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1

2) C++ Implementation

Give the number of rows as user input using cin and store it in a variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows as user input using
    // cin and store it in a variable.
    int numbrrows;
    cin >> numbrrows;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < numbrrows; m++) {
        // Loop from the parent loop iterator value to 0 in
        // decreasing order using another For loop(Nested
        // For Loop).
        for (int n = m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

6
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1

3) C Implementation

Give the number of rows as user input using scanf and store it in a variable.

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numbrrows;
    scanf("%d", &numbrrows);
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < numbrrows; m++) {
        // Loop from the parent loop iterator value to 0 in
        // decreasing order using another For loop(Nested
        // For Loop).
        for (int n = m; n > 0; n--) {
            // Print the iterator value of the inner for
            // loop.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

6
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1

Related Programs: