Author name: Vikram Chiluka

Program to Sort words in Alphabetic Order

Python Program to Sort words in Alphabetic Order

In the previous article, we have discussed Python Program to Find Longest Word from Sentence
Given a string and the task is to sort all words in a given sentence in alphabetic order.

sort() method :

By default, the sort() method sorts the list in ascending order.

split() method:

The split() method in Python divides a string into a list of strings by breaking the string with the specified separator.

Examples:

Example1:

Input:

Given String = "Hello this is btechgeeks"

Output:

The all sorted words of given sentence in Alphabetic order :
Hello
btechgeeks
is
this

Example2:

Input:

Given String = "Good morning this is btechgeeks"

Output:

The all sorted words of given sentence in Alphabetic order :
Good
btechgeeks
is
morning
this

Program to Sort words in Alphabetic Order

Below are the ways to sort all words in a given sentence in alphabetic order.

Method #1: Using sort() Method (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Split the given string into a list of words using the split() function and store it in another variable say “wrd_lst”.
  • Sort the above-obtained word list using the sort() method.
  • Loop in the above list “wrd_lst” using the for loop.
  • Print the iterator value to sort all words in a given sentence in alphabetic order.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gven_str = "Hello this is btechgeeks"
# Split the given string into list of words using the split() function and store
# it in another variable say "wrd_lst".
wrd_lst = gven_str.split()
# Sort the above obtained word list using the sort() method.
wrd_lst.sort()
# Loop in the above list "wrd_lst" using the for loop.
print("The all sorted words of given sentence in Alphabetic order :")
for wrd in wrd_lst:
  # Print the iterator value to sort all words in a given sentence in alphabetic order.
    print(wrd)

Output:

The all sorted words of given sentence in Alphabetic order :
Hello
btechgeeks
is
this

Method #2: Using sort() Method (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Split the given string into a list of words using the split() function and store it in another variable say “wrd_lst”.
  • Sort the above-obtained word list using the sort() method.
  • Loop in the above list “wrd_lst” using the for loop.
  • Print the iterator value to sort all words in a given sentence in alphabetic order.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using input() function and store it in a variable.
gven_str = input("Enter some random string = ")
# Split the given string into list of words using the split() function and store
# it in another variable say "wrd_lst".
wrd_lst = gven_str.split()
# Sort the above obtained word list using the sort() method.
wrd_lst.sort()
# Loop in the above list "wrd_lst" using the for loop.
print("The all sorted words of given sentence in Alphabetic order :")
for wrd in wrd_lst:
  # Print the iterator value to sort all words in a given sentence in alphabetic order.
    print(wrd)

Output:

Enter some random string = Good morning this is btechgeeks
The all sorted words of given sentence in Alphabetic order :
Good
btechgeeks
is
morning
this

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Sort words in Alphabetic Order Read More »

Program to Find Volume and Surface Area of Sphere

Python Program to Find Volume and Surface Area of Sphere

In the previous article, we have discussed Python Program to Find Volume and Surface Area of a Cube
Given the radius and the task is to find the volume and surface area of the given sphere.

Surface Area of the sphere:

A sphere resembles a basketball or the three-dimensional view of a circle. If we know the radius of the Sphere,

we can use the following formula to calculate the Surface Area of the Sphere:

The Surface Area of a Sphere = 4πr²

r= radius of the given sphere

The Volume of the sphere:

Volume is the amount of space inside the sphere. If we know the radius of the sphere, we can use the following formula to calculate the volume of the sphere:

The volume of a Sphere = 4πr³

Examples:

Example 1:

Input:

Given radius = 7.5

Output:

The Surface area of the given Sphere with the radius [ 7.5 ]= 706.500
The volume of the given Sphere with the radius [ 7.5 ]= 1766.250

Example 2:

Input:

Given radius = 3

Output:

The Surface area of the given Sphere with the radius [ 3 ]= 113.040
The volume of the given Sphere with the radius [ 3 ]= 113.040

Program to Find Volume and Surface Area of Sphere

Below are the ways to find the volume and surface area of the given sphere:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the radius of a sphere as static input and store it in a variable.
  • Take a variable and initialize it with the “pi” value i.e. 3.14 and store it in another variable.
  • Calculate the surface area of the given sphere using the above mathematical formula and store it in another variable.
  • Calculate the volume of the given sphere using the above mathematical formula and store it in another variable.
  • Print the surface area of the given sphere.
  • Print the volume of the given sphere.
  • The Exit of the Program.

Below is the implementation:

# Give the radius of a sphere  as static input and store it in a variable.
gvn_radis = 7.5
# Take a variable and initialize it with the "pi" value i.e. 3.14 and
# store it in another variable.
standard_pi_val = 3.14
# Calculate the surface area of the given sphere using the above mathematical formula
# and store it in another variable.
surf_area = 4 * standard_pi_val * gvn_radis * gvn_radis
# Calculate the volume of the given sphere using the above mathematical formula and
# store it in another variable.
sphre_volm = (4 / 3) * standard_pi_val * gvn_radis * gvn_radis * gvn_radis
# Print the surface area of the given sphere.
print(
    "The Surface area of the given Sphere with the radius [", gvn_radis, "]= %.3f" % surf_area)
# Print the volume of the given sphere.
print(
    "The volume of the given Sphere with the radius [", gvn_radis, "]= %.3f" % sphre_volm)

Output:

The Surface area of the given Sphere with the radius [ 7.5 ]= 706.500
The volume of the given Sphere with the radius [ 7.5 ]= 1766.250

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the radius of a sphere as user input using the float(input()) function and store it in a variable
  • Take a variable and initialize it with the “pi” value i.e. 3.14 and store it in another variable.
  • Calculate the surface area of the given sphere using the above mathematical formula and store it in another variable.
  • Calculate the volume of the given sphere using the above mathematical formula and store it in another variable.
  • Print the surface area of the given sphere.
  • Print the volume of the given sphere.
  • The Exit of the Program.

Below is the implementation:

# Give the radius of a sphere as user input using the float(input()) function
# and store it in a variable
gvn_radis = float(input("Enter some random variable = "))
# Take a variable and initialize it with the "pi" value i.e. 3.14 and
# store it in another variable.
standard_pi_val = 3.14
# Calculate the surface area of the given sphere using the above mathematical formula
# and store it in another variable.
surf_area = 4 * standard_pi_val * gvn_radis * gvn_radis
# Calculate the volume of the given sphere using the above mathematical formula and
# store it in another variable.
sphre_volm = (4 / 3) * standard_pi_val * gvn_radis * gvn_radis * gvn_radis
# Print the surface area of the given sphere.
print(
    "The Surface area of the given Sphere with the radius [", gvn_radis, "]= %.3f" % surf_area)
# Print the volume of the given sphere.
print(
    "The volume of the given Sphere with the radius [", gvn_radis, "]= %.3f" % sphre_volm)

Output:

Enter some random variable = 9
The Surface area of the given Sphere with the radius [ 9.0 ]= 1017.360
The volume of the given Sphere with the radius [ 9.0 ]= 3052.080

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find Volume and Surface Area of Sphere Read More »

Program to Find Volume and Surface Area of a Cuboid

Python Program to Find Volume and Surface Area of a Cuboid

In the previous article, we have discussed Python Program to Sort words in Alphabetic Order
Cuboid:

A Cuboid is a three-dimensional object made up of six Rectangles. All of the opposing faces (Top and Bottom) are the same.

A Cuboid’s Surface Area : 

The Total Surface Area of a Cuboid is the sum of the areas of the Cuboid’s six rectangles. If we know the Cuboid’s length, width, and height, we can use the formula to calculate its Total Surface Area:

Top and bottom surface area = lw + lw = 2lw

Front and back surface area = lh + lh = 2lh

Both sides’ area = wh + wh = 2wh

A Cuboid’s Total Surface Area is the sum of its six faces. So, in order to calculate the final Surface Area, we must add all of these areas together.

A Cuboid’s Total Surface Area = 2lw + 2lh + 2wh

It is the same as:

Formula for Surface Area of a cuboid = 2 (lw + lh + wh).

A Cuboid’s Volume :
Volume is the amount of space inside the Cuboid. If we know the Cuboid’s length, width, and height, we can use the following formula to calculate its volume:

Cuboid Volume = Length * Breadth * Height

A Cuboid’s Volume = lbh

A Cuboid’s Lateral Surface Area = 2h (l + w).

Given the length, width, and height of a cuboid, the task is to calculate the volume and surface of a given cuboid.

Examples:

Example 1:

Input:

Given length = 1
Given width = 2
Given height = 3

Output:

The given surface Area of a Cuboid with given length,width,height[ 1 2 3 ]= 22.000
The given volume of a Cuboid with given length,width,height[ 1 2 3 ]= 6.000
The given lateral surface Area of a Cuboid with given length,width,height[ 1 2 3 ]= 18.000

Example 2:

Input:

Given length = 2.5
Given width = 5
Given height = 3

Output:

The given surface Area of a Cuboid with given length,width,height[ 2.5 5.0 3.0 ]= 70.000
The given volume of a Cuboid with given length,width,height[ 2.5 5.0 3.0 ]= 37.500
The given lateral surface Area of a Cuboid with given length,width,height[ 2.5 5.0 3.0 ]= 45.000

Program to Find Volume and Surface Area of a Cuboid

Below are the ways to calculate the volume and surface of a given cuboid.

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the length of a cuboid as static input and store it in a variable.
  • Give the width of a cuboid as static input and store it in another variable.
  • Give the height of a cuboid as static input and store it in another variable.
  • Calculate the surface area of a given cuboid using the above given mathematical formula and store it in another variable.
  • Calculate the volume of a given cuboid using the above given mathematical formula and store it in another variable.
  • Calculate the lateral surface area of a given cuboid using the above given mathematical formula and store it in another variable.
  • Print the surface area of a given cuboid.
  • Print the volume of a given cuboid.
  • Print the lateral surface area of a given cuboid.
  • The Exit of the Program.

Below is the implementation:

# Give the length of a cuboid as static input and store it in a variable.
gvn_len = 1
# Give the width of a cuboid as static input and store it in another variable.
gvn_widt = 2
# Give the height of a cuboid as static input and store it in another variable.
gvn_heigt = 3
# Calculate the surface area of a given cuboid using the above given mathematical formula
# and store it in another variable.
surf_area = 2 * (gvn_len * gvn_widt + gvn_len *
                 gvn_heigt + gvn_widt * gvn_heigt)
# Calculate the volume of a given cuboid using the above given mathematical formula
# and store it in another variable.
Vol = gvn_len * gvn_widt * gvn_heigt
# Calculate the lateral surface area of a given cuboid using the above given mathematical
# formula and store it in another variable.
Laterl_surfcarea = 2 * gvn_heigt * (gvn_len + gvn_widt)
# Print the surface area of a given cuboid.
print(
    "The given surface Area of a Cuboid with given length,width,height[", gvn_len, gvn_widt, gvn_heigt, "]= %.3f" % surf_area)
# Print the volume of a given cuboid.
print(
    "The given volume of a Cuboid with given length,width,height[", gvn_len, gvn_widt, gvn_heigt, "]= %.3f" % Vol)
# Print the lateral surface area of a given cuboid.
print(
    "The given lateral surface Area of a Cuboid with given length,width,height[", gvn_len, gvn_widt, gvn_heigt, "]= %.3f" % Laterl_surfcarea)

Output:

The given surface Area of a Cuboid with given length,width,height[ 1 2 3 ]= 22.000
The given volume of a Cuboid with given length,width,height[ 1 2 3 ]= 6.000
The given lateral surface Area of a Cuboid with given length,width,height[ 1 2 3 ]= 18.000

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the length of a cuboid as user input using the float(input()) function and store it in a variable.
  • Give the width of a cuboid as user input using the float(input()) function and store it in another variable.
  • Give the height of a cuboid as user input using the float(input()) function and store it in another variable.
  • Calculate the surface area of a given cuboid using the above given mathematical formula and store it in another variable.
  • Calculate the volume of a given cuboid using the above given mathematical formula and store it in another variable.
  • Calculate the lateral surface area of a given cuboid using the above given mathematical formula and store it in another variable.
  • Print the surface area of a given cuboid.
  • Print the volume of a given cuboid.
  • Print the lateral surface area of a given cuboid.
  • The Exit of the Program.

Below is the implementation:

# Give the length of a cuboid as user input using the float(input()) function and
# store it in a variable.
gvn_len = float(input("Enter some random number = "))
# Give the width of a cuboid as user input using the float(input()) function and
# store it in another variable.
gvn_widt = float(input("Enter some random number = "))
# Give the height of a cuboid as user input using the float(input()) function
# and store it in another variable.
gvn_heigt = float(input("Enter some random number = "))
# Calculate the surface area of a given cuboid using the above given mathematical formula
# and store it in another variable.
surf_area = 2 * (gvn_len * gvn_widt + gvn_len *
                 gvn_heigt + gvn_widt * gvn_heigt)
# Calculate the volume of a given cuboid using the above given mathematical formula
# and store it in another variable.
Vol = gvn_len * gvn_widt * gvn_heigt
# Calculate the lateral surface area of a given cuboid using the above given mathematical
# formula and store it in another variable.
Laterl_surfcarea = 2 * gvn_heigt * (gvn_len + gvn_widt)
# Print the surface area of a given cuboid.
print(
    "The given surface Area of a Cuboid with given length,width,height[", gvn_len, gvn_widt, gvn_heigt, "]= %.3f" % surf_area)
# Print the volume of a given cuboid.
print(
    "The given volume of a Cuboid with given length,width,height[", gvn_len, gvn_widt, gvn_heigt, "]= %.3f" % Vol)
# Print the lateral surface area of a given cuboid.
print(
    "The given lateral surface Area of a Cuboid with given length,width,height[", gvn_len, gvn_widt, gvn_heigt, "]= %.3f" % Laterl_surfcarea)

Output:

Enter some random number = 2.5
Enter some random number = 5
Enter some random number = 3
The given surface Area of a Cuboid with given length,width,height[ 2.5 5.0 3.0 ]= 70.000
The given volume of a Cuboid with given length,width,height[ 2.5 5.0 3.0 ]= 37.500
The given lateral surface Area of a Cuboid with given length,width,height[ 2.5 5.0 3.0 ]= 45.000

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find Volume and Surface Area of a Cuboid Read More »

Program to Find Leaders in an ArrayList

Python Program to Find Leaders in an Array/List

In the previous article, we have discussed Python Program to Check Automorphic Number or Not
Leader:

If an element is greater than all of the elements on its right side, it is the leader. And the last element is always a leader.

Examples:

Example1:

Input:

Given list =[23, 11, 1, 7, 8, 6, 3]

Output:

The leaders of the given list [23, 11, 1, 7, 8, 6, 3] are :
23
11
8
6
3

Example2:

Input:

Given List =  [1, 2, 3, 7, 8, 6]

Output:

The leaders of the given list [1, 2, 3, 7, 8, 6] are :
8
6

Given a list, the task is to find all the leaders of the given list in python.

Program to Find Leaders in an Array/List in Python

Below are the ways to find all the leaders of the given list in python some of them are:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the list and store it in another variable.
  • Loop from 0 to the length of the list using the For loop.
  • Loop from parent loop iterator value to the length of the list using another Nested for loop(Inner For loop)
  • Check if the element at the index of the parent loop iterator value is less than or equal to the element at the index of the inner loop iterator value.(gvnlist [ m ] <= gvnlist [ n ] ) using the if conditional statement.
  • If it is true then break the inner loop using the break keyword.
  • After the end of the inner For loop check if the inner loop iterator value is equal to the length of the given list -1 using the if conditional statement.
  • If it is true then it is the leader so print it.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvnlstt = [23, 11, 1, 7, 8, 6, 3]
# Calculate the length of the list and store it in another variable.
lstleng = len(gvnlstt)
print('The leaders of the given list', gvnlstt, 'are :')
# Loop from 0 to the length of the list using the For loop.
for m in range(lstleng):
    # Loop from parent loop iterator value to the length of the list
    # using another Nested for loop(Inner For loop)
    for n in range(m+1, lstleng):
            # Check if the element at the index of the parent loop iterator value
        # is less than or equal to the element at the index of the inner loop iterator value.
        # (gvnlist[m] <= gvnlist[n]) using the if conditional statement.
        if (gvnlstt[m] <= gvnlstt[n]):
            # If it is true then break the inner loop using the break keyword.
            break

            # After the end of the inner For loop check if the inner loop iterator value
        # is equal to the length of the given list - 1 using the if conditional statement.
    if(n == lstleng-1):
        # If it is true then it is the leader so print it.
        print(gvnlstt[m])

Output:

The leaders of the given list [23, 11, 1, 7, 8, 6, 3] are :
23
11
8
6
3

Method #2: Using For loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the list and store it in another variable.
  • Loop from 0 to the length of the list using the For loop.
  • Loop from parent loop iterator value to the length of the list using another Nested for loop(Inner For loop)
  • Check if the element at the index of the parent loop iterator value is less than or equal to the element at the index of the inner loop iterator value.(gvnlist [ m ] <= gvnlist [ n ] ) using the if conditional statement.
  • If it is true then break the inner loop using the break keyword.
  • After the end of the inner For loop check if the inner loop iterator value is equal to the length of the given list -1 using the if conditional statement.
  • If it is true then it is the leader so print it.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlstt = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the list and store it in another variable.
lstleng = len(gvnlstt)
print('The leaders of the given list', gvnlstt, 'are :')
# Loop from 0 to the length of the list using the For loop.
for m in range(lstleng):
    # Loop from parent loop iterator value to the length of the list
    # using another Nested for loop(Inner For loop)
    for n in range(m+1, lstleng):
            # Check if the element at the index of the parent loop iterator value
        # is less than or equal to the element at the index of the inner loop iterator value.
        # (gvnlist[m] <= gvnlist[n]) using the if conditional statement.
        if (gvnlstt[m] <= gvnlstt[n]):
            # If it is true then break the inner loop using the break keyword.
            break

            # After the end of the inner For loop check if the inner loop iterator value
        # is equal to the length of the given list - 1 using the if conditional statement.
    if(n == lstleng-1):
        # If it is true then it is the leader so print it.
        print(gvnlstt[m])

Output:

Enter some random List Elements separated by spaces = 1 2 3 7 8 6
The leaders of the given list [1, 2, 3, 7, 8, 6] are :
8
6

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find Leaders in an Array/List Read More »

Program to Move all Negative Elements to End in Order with Extra Space Allowed

Python Program to Move all Negative Elements to End in Order with Extra Space Allowed

In the previous article, we have discussed Python Program to Split the Array/List and add the First Part to the End
Given a list that contains the negative and positive elements the task is to move all the negative elements to the end of the list.

Examples:

Example1:

Input:

Given List =[1, 9, -3, 6, 8, 11, 35, -5, -7, 10, -1, -2, -3]

Output:

The given list after moving negative elements to the end is [1, 9, 6, 8, 11, 35, 10, -3, -5, -7, -1, -2, -3]

Example2:

Input:

Given List = [-7 11 56 -3 -8 12 17 -25 -8 -11 -6 9 10]

Output:

The given list after moving negative elements to the end is [11, 56, 12, 17, 9, 10, -7, -3, -8, -25, -8, -11, -6]

Program to Move all Negative Elements to End in Order with Extra Space Allowed in Python

There are several ways to move all the negative elements to the end of the list some of them are:

Method #1: Using List Comprehension (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Using list comprehension and if conditional statements separate the positive numbers from the list and store it in a variable to say the positive list.
  • Using list comprehension and if conditional statements separate the negative numbers from the list and store it in a variable to say the negative list.
  • Add the positive list and negative list using the + operator and store it in another variable to say result list(This operation moves all the negative list).
  • Print the result list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvnlst = [1, 9, -3, 6, 8, 11, 35, -5, -7, 10, -1, -2, -3]
# Using list comprehension and if conditional statements
# separate the positive numbers from the list
# and store it in a variable to say the positive list.
pstivelist = [elemn for elemn in gvnlst if elemn >= 0]
# Using list comprehension and if conditional statements
# separate the negative numbers from the list and store it
# in a variable to say the negative list.
ngtivelist = [elemn for elemn in gvnlst if elemn < 0]
# Add the positive list and negative list using the + operator
# and store it in another variable to say result list
# (This operation moves all the negative list).
resltlist = pstivelist+ngtivelist
# Print the result list.
print('The given list after moving negative elements to the end is', resltlist)

Output:

The given list after moving negative elements to the end is [1, 9, 6, 8, 11, 35, 10, -3, -5, -7, -1, -2, -3]

Method #2: Using List Comprehension (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Using list comprehension and if conditional statements separate the positive numbers from the list and store it in a variable to say the positive list.
  • Using list comprehension and if conditional statements separate the negative numbers from the list and store it in a variable to say the negative list.
  • Add the positive list and negative list using the + operator and store it in another variable to say result list(This operation moves all the negative list).
  • Print the result list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlstt = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Using list comprehension and if conditional statements
# separate the positive numbers from the list
# and store it in a variable to say the positive list.
pstivelist = [elemn for elemn in gvnlstt if elemn >= 0]
# Using list comprehension and if conditional statements
# separate the negative numbers from the list and store it
# in a variable to say the negative list.
ngtivelist = [elemn for elemn in gvnlstt if elemn < 0]
# Add the positive list and negative list using the + operator
# and store it in another variable to say result list
# (This operation moves all the negative list).
resltlist = pstivelist+ngtivelist
# Print the result list.
print('The given list after moving negative elements to the end is', resltlist)

Output:

Enter some random List Elements separated by spaces = -7 11 56 -3 -8 12 17 -25 -8 -11 -6 9 10
The given list after moving negative elements to the end is [11, 56, 12, 17, 9, 10, -7, -3, -8, -25, -8, -11, -6]

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Move all Negative Elements to End in Order with Extra Space Allowed Read More »

Program to Find Isosceles Triangle Area

Python Program to Find Isosceles Triangle Area

In the previous article, we have discussed Python Program to Find Volume and Surface Area of Sphere
Isosceles triangle :

An isosceles triangle is a triangle with two equal-length sides in geometry.It is sometimes stated as having exactly two equal-length sides and other times as having at least two equal-length sides, with the latter containing the equilateral triangle as a particular scenario.

Formula :

The formula to find the area of an isosceles triangle = (y * math.sqrt((4 * x * x) – (y * y)))/4;

where x , y are the two side lengths of an isosceles triangle.

Given two sides of an isosceles triangle and the task is to find the area of the given isosceles triangle.

Examples:

Example1:

Input:

Given length of first side = 5
Given length of second side = 9

Output:

The Given Isosceles Triangle area with sides [ 5 , 9 ]= 9.808

Example2:

Input:

Given length of first side =  12
Given length of second side = 17.2

Output:

The Given Isosceles Triangle area with sides [ 12 , 17.2 ]= 71.973

Program to Find Isosceles Triangle Area

Below are the ways to find the area of the given isosceles triangle:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the length of the first side of an isosceles triangle as static input and store it in a variable.
  • Give the length of the second side of an isosceles triangle as static input and store it in another variable.
  • Calculate the length of the given isosceles triangle using math.sqrt() function and the above given mathematical formula.
  • Store it in another variable.
  • Print the area of the given isosceles triangle.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the length of the first side of an isosceles triangle as static input
# and store it in a variable.
fst_side = 5
# Give the length of the second side of an isosceles triangle as static input
# and store it in another variable.
secnd_side = 9
# Calculate the length of the given isosceles triangle using math.sqrt() function
# and the above given mathematical formula.
# Store it in another variable.
area_isosce_tringl = (
    secnd_side * math.sqrt((4 * fst_side * fst_side) - (secnd_side * secnd_side)))/4
# Print the area of the given isosceles triangle.
print("The Given Isosceles Triangle area with sides [",
      fst_side, ",", secnd_side, "]= %.3f" % area_isosce_tringl)

Output:

The Given Isosceles Triangle area with sides [ 5 , 9 ]= 9.808

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the length of the first side of an isosceles triangle as user input using the float(input()) function and store it in a variable.
  • Give the length of the second side of an isosceles triangle as user input using the float(input()) function and store it in another variable.
  • Calculate the length of the given isosceles triangle using math.sqrt() function and the above given mathematical formula.
  • Store it in another variable.
  • Print the area of the given isosceles triangle.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the length of the first side of an isosceles triangle as user input using the float(input()) 
#function and store it in a variable.
fst_side = float(input("Enter some random number = "))
# Give the length of the second side of an isosceles triangle as user input using the float(input())
#function and store it in another variable.
secnd_side =  float(input("Enter some random number = "))
# Calculate the length of the given isosceles triangle using math.sqrt() function
# and the above given mathematical formula.
# Store it in another variable.
area_isosce_tringl = (
    secnd_side * math.sqrt((4 * fst_side * fst_side) - (secnd_side * secnd_side)))/4
# Print the area of the given isosceles triangle.
print("The Given Isosceles Triangle area with sides [",
      fst_side, ",", secnd_side, "]= %.3f" % area_isosce_tringl)

Output:

Enter some random number = 15
Enter some random number = 22.5
The Given Isosceles Triangle area with sides [ 15.0 , 22.5 ]= 111.618

The area is isosceles is calculated by the above formula.

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find Isosceles Triangle Area Read More »

Python Program to Find Volume and Surface Area of a Cube

In the previous article, we have discussed Python Program to Find Sum of Arithmetic Progression Series
Surface Area of a Cube :

If we know the length of an edge in a Cube, we can use the following formula to calculate the surface area of a Cube:

Cube Surface Area = 6S²

In which  S= length of any side of a Cube

A square’s area = S². Surface Area of a Cube = 6S²  because the Cube is made up of 6 equal squares.

The volume of a Cube :

Volume refers to the amount of space inside the Cube. If we know the length of any edge of a Cube, we can use the following

Formula to calculate the Volume of the Cube:  S*S*S*

A Cube’s Lateral Surface Area = 4 * (S * S)
Given the side of a cube and the task is to find the surface area, volume, and lateral surface for a given side of a cube.

Examples:

Example 1:

Input:

Given side length of a cube = 12

Output:

The given surface Area of a Cube with side length [ 12 ]= 864.000
The given volume of a Cube with side length [ 12 ]= 1728.000
The given lateral surface area of a Cube with side lngth [ 12 ]= 576.000

Example 2:

Input:

Given side length of a cube = 4

Output:

The given surface Area of a Cube with side length [ 4 ]= 96.000
The given volume of a Cube with side length [ 4 ]= 64.000
The given lateral surface area of a Cube with side lngth [ 4 ]= 64.000

Program to Find Volume and Surface Area of a Cube in Python

Below are the ways to find the surface area, volume, and lateral surface for a given side of a cube:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the length of the side of a cube as static input and store it in a variable.
  • Calculate the surface area of a given cube using the above given mathematical formula and store it in another variable.
  • Calculate the volume of a given cube using the above given mathematical formula and store it in another variable.
  • Calculate the lateral surface area of a given cube using the above given mathematical formula and store it in another variable.
  • Print the surface area of a given cube.
  • Print the volume of a given cube.
  • Print the lateral surface area of a given cube.
  • The Exit of the Program.
 Below is the implementation:
# Give the length of the side of a cube as static input and store it in a variable.
cube_side = 7
# Calculate the surface area of a given cube using the above given mathematical formula
# and store it in another variable.
surf_area = 6 * (cube_side * cube_side)
# Calculate the volume of a given cube using the above given mathematical formula and
# store it in another variable.
Volum = cube_side * cube_side * cube_side
# Calculate the lateral surface area of a given cube using the above given mathematical
# formula and store it in another variable.
Laterl_surfcarea = 4 * (cube_side * cube_side)
# Print the surface area of a given cube.
print(
    "The given surface Area of a Cube with side length [", cube_side, "]= %.3f" % surf_area)
# Print the volume of a given cube.
print(
    "The given volume of a Cube with side length [", cube_side, "]= %.3f" % Volum)
# Print the lateral surface area of a given cube.
print(
    "The given lateral surface area of a Cube with side lngth [", cube_side, "]= %.3f" % Laterl_surfcarea)

Output:

The given surface Area of a Cube with side length [ 7 ]= 294.000
The given volume of a Cube with side length [ 7 ]= 343.000
The given lateral surface area of a Cube with side lngth [ 7 ]= 196.000

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the length of the side of a cube as user input using the float(input()) function and store it in a variable.
  • Calculate the surface area of a given cube using the above given mathematical formula and store it in another variable.
  • Calculate the volume of a given cube using the above given mathematical formula and store it in another variable.
  • Calculate the lateral surface area of a given cube using the above given mathematical formula and store it in another variable.
  • Print the surface area of a given cube.
  • Print the volume of a given cube.
  • Print the lateral surface area of a given cube.
  • The Exit of the Program.

 Below is the implementation:

# Give the length of the side of a cube as user input using the float(input()) function
# and store it in a variable.
cube_side = float(input("Enter some random number = "))
# Calculate the surface area of a given cube using the above given mathematical formula
# and store it in another variable.
surf_area = 6 * (cube_side * cube_side)
# Calculate the volume of a given cube using the above given mathematical formula and
# store it in another variable.
Volum = cube_side * cube_side * cube_side
# Calculate the lateral surface area of a given cube using the above given mathematical
# formula and store it in another variable.
Laterl_surfcarea = 4 * (cube_side * cube_side)
# Print the surface area of a given cube.
print(
    "The given surface Area of a Cube with side length [", cube_side, "]= %.3f" % surf_area)
# Print the volume of a given cube.
print(
    "The given volume of a Cube with side length [", cube_side, "]= %.3f" % Volum)
# Print the lateral surface area of a given cube.
print(
    "The given lateral surface area of a Cube with side lngth [", cube_side, "]= %.3f" % Laterl_surfcarea)

Output:

Enter some random number = 5.5
The given surface Area of a Cube with side length [ 5.5 ]= 181.500
The given volume of a Cube with side length [ 5.5 ]= 166.375
The given lateral surface area of a Cube with side lngth [ 5.5 ]= 121.000

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find Volume and Surface Area of a Cube Read More »

Program to Count Divisors of Factorial

Python Program to Count Divisors of Factorial

In the previous article, we have discussed Python Program to Reorder a List According to Given Indexes
Factorial:

The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, denoted by n! in mathematics:

n! = n * (n – 1) *(n – 2) * . . . . . . . . . . 3 * 2 * 1.

4 != 4 * 3 * 2 *1= 24

Given a number and the task is to count divisors of factorial of a given number.

For example :

Let N=6

The divisors of factorial of a given number{ 6 }: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 30 36 40 45 48 60 72 80 90 120 144 180 240 360 720

The count of divisors of factorial of a given number{ 6 }= 30

Examples:

Example 1:

Input:

Given Number = 7

Output:

The divisors of factorial of a given number{ 7 }:
1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 28 30 35 36 40 42 45 48 56 60 63 70 72 80 84 90 105 112 120 126 140 144 168 180 210 240 252 280 315 336 360 420 504 560 630 720 840 1008 1260 1680 2520 5040 
The count of divisors of factorial of a given number{ 7 }= 60

Example 2:

Input:

Given Number = 4

Output:

The divisors of factorial of a given number{ 4 }:
1 2 3 4 6 8 12 24 
The count of divisors of factorial of a given number{ 4 }= 8

Program to Count Divisors of Factorial in Python

Below are the ways to count divisors of factorial of a given number.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Take a variable, initialize it with the value ‘1’, and store it in another variable say ‘factorial’.
  • Loop from 1 to the given number using the for loop.
  • Calculate the factorial of a given number by multiplying the above variable “factorial” with the iterator and store it in the same variable ‘factorial’.
  • Take a variable, initialize it with the value ‘0’, and store it in another variable say ‘c’.
  • Loop from 1 to the above obtained factorial number using the for loop.
  • Check if the value of factorial modulus iterator value is equal to 0 using the if conditional statement.
  • If the statement is true, print the iterator value and increment the value of ‘c’.
  • Store it in the same variable ‘c’.
  • Print the count of divisors of factorial of a given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
num = 5
# Take a variable, initialize it with the value '1', and store it in another
# variable say 'factorial'.
factorl = 1
# Loop from 1 to the given number using the for loop.
for i in range(1, num+1):
 # Calculate the factorial of a given number by multiplying the above variable "factorial"
    # with the iterator and store it in the same variable 'factorial'.
    factorl = factorl*i
# Take a variable, initialize it with the value '0', and store it in another variable
# say 'c'.
c = 0
print("The divisors of factorial of a given number{", num, "}:")
# Loop from 1 to the above obtained factorial number using the for loop.
for i in range(1, factorl+1):
  # Check if the value of factorial modulus iterator value is equal to 0 using
    # the if conditional statement.
    if(factorl % i == 0):
     # If the statement is true, print the iterator value and increment the value of 'c'.
        # Store it in the same variable 'c'.
        print(i, end=" ")
        c = c+1
print()
# Print the count of divisors of factorial of a given number.
print("The count of divisors of factorial of a given number{", num, "}=", c)

Output:

The divisors of factorial of a given number{ 5 }:
1 2 3 4 5 6 8 10 12 15 20 24 30 40 60 120 
The count of divisors of factorial of a given number{ 5 }= 16

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Take a variable, initialize it with the value ‘1’, and store it in another variable say ‘factorial’.
  • Loop from 1 to the given number using the for loop.
  • Calculate the factorial of a given number by multiplying the above variable “factorial” with the iterator and store it in the same variable ‘factorial’.
  • Take a variable, initialize it with the value ‘0’, and store it in another variable say ‘c’.
  • Loop from 1 to the above obtained factorial number using the for loop.
  • Check if the value of factorial modulus iterator value is equal to 0 using the if conditional statement.
  • If the statement is true, print the iterator value and increment the value of ‘c’.
  • Store it in the same variable ‘c’.
  • Print the count of divisors of factorial of a given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
num = int(input("Enter some random number = "))
# Take a variable, initialize it with the value '1', and store it in another
# variable say 'factorial'.
factorl = 1
# Loop from 1 to the given number using the for loop.
for i in range(1, num+1):
 # Calculate the factorial of a given number by multiplying the above variable "factorial"
    # with the iterator and store it in the same variable 'factorial'.
    factorl = factorl*i
# Take a variable, initialize it with the value '0', and store it in another variable
# say 'c'.
c = 0
print("The divisors of factorial of a given number{", num, "}:")
# Loop from 1 to the above obtained factorial number using the for loop.
for i in range(1, factorl+1):
  # Check if the value of factorial modulus iterator value is equal to 0 using
    # the if conditional statement.
    if(factorl % i == 0):
     # If the statement is true, print the iterator value and increment the value of 'c'.
        # Store it in the same variable 'c'.
        print(i, end=" ")
        c = c+1
print()
# Print the count of divisors of factorial of a given number.
print("The count of divisors of factorial of a given number{", num, "}=", c)

Output:

Enter some random number = 3
The divisors of factorial of a given number{ 3 }:
1 2 3 6 
The count of divisors of factorial of a given number{ 3 }= 4

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Count Divisors of Factorial Read More »

Program to Find Volume and Surface Area of a Cone

Python Program to Find Volume and Surface Area of a Cone

In the previous article, we have discussed Python Program to Calculate Sum of Series 1³+2³+3³+….+n³
The surface area of a cone : 

Formula to calculate its surface area:
Surface Area = Area of a Cone + Area of a Circle
i.e. Surface Area of cone = πrl + πr²
In which  ‘r’ denotes radius and

‘l’ denotes slant (Length of an edge from the top of the cone to edge of a cone)

If we know the radius and height of a cone, we can use the following formula to calculate its surface area:
Surface Area = πr² +πr √h² + r²
It can also be written as:
Surface Area of a cone =  πr (r+√h² + r²)

Because the radius, height, and slant combine to form a right-angled triangle. So, using Pythagoras’ theorem:

l² = h² + r²
l = √h² + r²

The volume of a cone :

Volume refers to the amount of space inside the Cone. If we know the radius and height of the cone, we can use the following formula to calculate the volume:
The volume of a cone= 1/3 πr²h  (where h = height of a cone)

A Cone’s Lateral Surface Area = Ï€rl

Given the radius and height of a cone, the task is to find the surface area, volume, and lateral surface for a given cone.

Examples:

Example 1:

Input:

Given radius = 6.5
Given height = 10

Output:

The given cone's slant height = 11.927
The given surface Area of a cone with given radius,height[ 6.5 10 ]= 376.283
The given volume of a cone with given radius,height[ 6.5 10 ]= 442.441
The given lateral surface Area of a cone with given radius,height[ 6.5 10 ]= 243.551

Example 2:

Input:

Given radius =  4
Given height = 8

Output:

The given cone's slant height = 8.944
The given surface Area of a cone with given radius,height[ 4 8 ]= 162.663
The given volume of a cone with given radius,height[ 4 8 ]= 134.041
The given lateral surface Area of a cone with given radius,height[ 4 8 ]= 112.397

Program to Find Volume and Surface Area of a Cone

Below are the ways to find the surface area, volume, and lateral surface for a given cone.

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the radius of a cone as static input and store it in a variable.
  • Give the height of a cone as static input and store it in another variable.
  • Calculate the Slant height of a given cone using the above given mathematical formula and math.sqrt() function and store it in another variable.
  • Calculate the surface area of a given cone using the above given mathematical formula and math.pi function
  • Store it in another variable.
  • Calculate the volume of a given cone using the above given mathematical formula and math.pi function
  • Store it in another variable.
  • Calculate the lateral surface area of a given cone using the above given mathematical formula and math.pi function.
  • Store it in another variable.
  • Print the slant height of a given cone.
  • Print the surface area of a given cone.
  • Print the volume of a given cone.
  • Print the lateral surface area of a given cone.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the radius of a cone as static input and store it in a variable.
gvn_rad = 6.5
# Give the height of a cone as static input and store it in another variable.
gvn_heigt = 10
# Calculate the Slant height of a given cone using the above given mathematical formula
# and math.sqrt() function and store it in another variable.
slant_l = math.sqrt(gvn_rad * gvn_rad + gvn_heigt * gvn_heigt)
# Calculate the surface area of a given cone using the above given mathematical formula and math.pi function
# Store it in another variable.
surf_area = math.pi * gvn_rad * (gvn_rad + slant_l)
# Calculate the volume of a given cone using the above given mathematical formula and
# math.pi function
# Store it in another variable.
Vol = (1.0/3) * math.pi * gvn_rad * gvn_rad * gvn_heigt
# Calculate the lateral surface area of a given cone using the above given mathematical
# formula and math.pi function.
# Store it in another variable.
Laterl_surfcarea = math.pi * gvn_rad * slant_l
# Print the slant height of a given cone.
print("The given cone's slant height = %.3f" % slant_l)
# Print the surface area of a given cone.
print(
    "The given surface Area of a cone with given radius,height[", gvn_rad, gvn_heigt, "]= %.3f" % surf_area)
# Print the volume of a given cone.
print(
    "The given volume of a cone with given radius,height[", gvn_rad, gvn_heigt, "]= %.3f" % Vol)
# Print the lateral surface area of a given cone.
print(
    "The given lateral surface Area of a cone with given radius,height[", gvn_rad, gvn_heigt, "]= %.3f " % Laterl_surfcarea)

Output:

The given cone's slant height = 11.927
The given surface Area of a cone with given radius,height[ 6.5 10 ]= 376.283
The given volume of a cone with given radius,height[ 6.5 10 ]= 442.441
The given lateral surface Area of a cone with given radius,height[ 6.5 10 ]= 243.551

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the radius of a cone as user input using the float(input()) function and store it in a variable.
  • Give the height of a cone as user input using the float(input()) function and store it in another variable.
  • Calculate the Slant height of a given cone using the above given mathematical formula and math.sqrt() function and store it in another variable.
  • Calculate the surface area of a given cone using the above given mathematical formula and math.pi function
  • Store it in another variable.
  • Calculate the volume of a given cone using the above given mathematical formula and math.pi function
  • Store it in another variable.
  • Calculate the lateral surface area of a given cone using the above given mathematical formula and math.pi function.
  • Store it in another variable.
  • Print the slant height of a given cone.
  • Print the surface area of a given cone.
  • Print the volume of a given cone.
  • Print the lateral surface area of a given cone.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the radius of a cone user input using the float(input()) function and store it in a variable.
gvn_rad = float(input("Enter some random number = "))
# Give the height of a cone user input using the float(input()) function and store it in another variable.
gvn_heigt = float(input("Enter some random number = "))
# Calculate the Slant height of a given cone using the above given mathematical formula
# and math.sqrt() function and store it in another variable.
slant_l = math.sqrt(gvn_rad * gvn_rad + gvn_heigt * gvn_heigt)
# Calculate the surface area of a given cone using the above given mathematical formula and math.pi function
# Store it in another variable.
surf_area = math.pi * gvn_rad * (gvn_rad + slant_l)
# Calculate the volume of a given cone using the above given mathematical formula and
# math.pi function
# Store it in another variable.
Vol = (1.0/3) * math.pi * gvn_rad * gvn_rad * gvn_heigt
# Calculate the lateral surface area of a given cone using the above given mathematical
# formula and math.pi function.
# Store it in another variable.
Laterl_surfcarea = math.pi * gvn_rad * slant_l
# Print the slant height of a given cone.
print("The given cone's slant height = %.3f" % slant_l)
# Print the surface area of a given cone.
print(
    "The given surface Area of a cone with given radius,height[", gvn_rad, gvn_heigt, "]= %.3f" % surf_area)
# Print the volume of a given cone.
print(
    "The given volume of a cone with given radius,height[", gvn_rad, gvn_heigt, "]= %.3f" % Vol)
# Print the lateral surface area of a given cone.
print(
    "The given lateral surface Area of a cone with given radius,height[", gvn_rad, gvn_heigt, "]= %.3f " % Laterl_surfcarea)

Output:

Enter some random number = 4.5
Enter some random number = 2
The given cone's slant height = 4.924
The given surface Area of a cone with given radius,height[ 4.5 2.0 ]= 133.235
The given volume of a cone with given radius,height[ 4.5 2.0 ]= 42.412
The given lateral surface Area of a cone with given radius,height[ 4.5 2.0 ]= 69.617

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find Volume and Surface Area of a Cone Read More »

Program for Product of Maximum in First array and Minimum in Second

Python Program for Product of Maximum in First array and Minimum in Second

In the previous article, we have discussed Python Program to Count Divisors of Factorial
Given two lists, the task is to find the product of maximum in the given first list and minimum in the given second list.

Examples:

Example 1:

Input:

Given the First List = [1, 2, 3, 4, 5]
Given the Second List = [2, 6, 7, 8, 9, 1]

Output:

The maximum element in given first list [1, 2, 3, 4, 5] = 5
The minimum element in given second list [2, 6, 7, 8, 9, 1] = 1
The product of max_in_fstlst and min_in_seclst [ 5 * 1 ] = 5

Example 2:

Input:

Given the First List = [10, 20, 30, 40, 50] 
Given the Second List = [1, 2, 3, 4, 5]

Output:

The maximum element in given first list [10, 20, 30, 40, 50] = 50
The minimum element in given second list [1, 2, 3, 4, 5] = 1
The product of max_in_fstlst and min_in_seclst [ 50 * 1 ] = 50

Program for Product of Maximum in First array and Minimum in Second in Python

Below are the ways to find the product of maximum in the given first list and minimum in the given second list.

Method #1: Using max() and min() Functions (Static Input)

Approach:

  • Give the first list as static input and store it in a variable.
  • Give the second list as static input and store it in another variable.
  • Find out the maximum element in the given first list using the built-in max() method and store it in another variable.
  • Find out the minimum element in the given second list using the built-in min() method and store it in another variable.
  • Multiply both the above obtained maximum element in the first list and minimum element in the given second list and store it in another variable.
  • Print the maximum element in the given first .
  • Print the minimum element in the given second list.
  • Print the Product of maximum element in the given first and minimum element in the given second list.
  • The Exit of the Program.

Below is the implementation:

# Give the first list as static input and store it in a variable.
fst_lst = [1, 2, 3, 4, 5]
# Give the second list as static input and store it in another variable.
secnd_lst = [2, 6, 7, 8, 9, 1]
# Find out the maximum element in the given first list using the built-in max() method
# and store it in another variable.
max_in_fstlst = max(fst_lst)
# Find out the minimum element in the given second list using the built-in min() method
# and store it in another variable.
min_in_seclst = min(secnd_lst)
# Multiply both the above obtained maximum element in the first list and minimum element
# in the given second list and store it in another variable.
prodct = max_in_fstlst*min_in_seclst
# Print the maximum element in the given first .
print("The maximum element in given first list", fst_lst, "=", max_in_fstlst)
# Print the minimum element in the given second list.
print("The minimum element in given second list", secnd_lst, "=", min_in_seclst)
# Print the Product of maximum element in the given first and minimum element in
# the given second list.
print("The product of max_in_fstlst and min_in_seclst [",
      max_in_fstlst, "*", min_in_seclst, "] =", prodct)

Output:

The maximum element in given first list [1, 2, 3, 4, 5] = 5
The minimum element in given second list [2, 6, 7, 8, 9, 1] = 1
The product of max_in_fstlst and min_in_seclst [ 5 * 1 ] = 5

Method #2: Using max() and min() Functions (User Input)

Approach:

  • Give the first list as user input using list(),map(),input(),and split() functions and store it in a variable
  • Give the second list as user input using list(),map(),input(),and split() functions and store it in another variable.
  • Find out the maximum element in the given first list using the built-in max() method and store it in another variable.
  • Find out the minimum element in the given second list using the built-in min() method and store it in another variable.
  • Multiply both the above obtained maximum element in the first list and minimum element in the given second list and store it in another variable.
  • Print the maximum element in the given first .
  • Print the minimum element in the given second list.
  • Print the Product of maximum element in the given first and minimum element in the given second list.
  • The Exit of the Program.

Below is the implementation:

# Give the first list as user input using list(),map(),input(),and split() functions and
# store it in a variable
fst_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the second list as user input using list(),map(),input(),and split() functions and
# store it in another variable.
secnd_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Find out the maximum element in the given first list using the built-in max() method
# and store it in another variable.
max_in_fstlst = max(fst_lst)
# Find out the minimum element in the given second list using the built-in min() method
# and store it in another variable.
min_in_seclst = min(secnd_lst)
# Multiply both the above obtained maximum element in the first list and minimum element
# in the given second list and store it in another variable.
prodct = max_in_fstlst*min_in_seclst
# Print the maximum element in the given first .
print("The maximum element in given first list", fst_lst, "=", max_in_fstlst)
# Print the minimum element in the given second list.
print("The minimum element in given second list", secnd_lst, "=", min_in_seclst)
# Print the Product of maximum element in the given first and minimum element in
# the given second list.
print("The product of max_in_fstlst and min_in_seclst [",
      max_in_fstlst, "*", min_in_seclst, "] =", prodct)

Output:

Enter some random List Elements separated by spaces = 4 1 4 2 6 8
Enter some random List Elements separated by spaces = 8 2 4 1 0 -1
The maximum element in given first list [4, 1, 4, 2, 6, 8] = 8
The minimum element in given second list [8, 2, 4, 1, 0, -1] = -1
The product of max_in_fstlst and min_in_seclst [ 8 * -1 ] = -8

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program for Product of Maximum in First array and Minimum in Second Read More »