Python

Program for Maximum Area of Quadrilateral

Python Program for Maximum Area of Quadrilateral

Given four sides of a quadrilateral, the task is to get the maximum area of the given quadrilateral for the given four sides in python.

Quadrilateral:

In geometry, a quadrilateral is a closed shape formed by joining four points, any three of which are non-collinear. A quadrilateral is made up of four sides, four angles, and four vertices. The term ‘quadrilateral’ is derived from the Latin words ‘quadra’ (four) and ‘Latus’ (sides). A quadrilateral’s four sides may or may not be equal.

Formula:

The formula to calculate the maximum area of the given quadrilateral is :

K={sqrt {(s-a)(s-b)(s-c)(s-d)}}

This is known as  Brahmagupta’s Formula.

s=(a+b+c+d)/2

where a, b, c, d are the four sides of a quadrilateral.

Examples:

Example1:

Input:

Given first side = 2
Given second side = 1
Given third side =  3
Given fourth side =  4

Output:

The maximimum area of quadrilateral for the given four sides { 2 , 1 , 3 , 4 } =  4.898979485566356

Example2:

Input:

Given first side = 5
Given second side = 3
Given third side = 5
Given fourth side = 2

Output:

The maximimum area of quadrilateral for the given four sides { 5 , 3 , 5 , 2 } =  12.43734296383275

Program for Maximum Area of Quadrilateral in Python

Below are the ways to get the maximum area of the given quadrilateral for the given four sides in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the first side as static input and store it in a variable.
  • Give the second side as static input and store it in another variable.
  • Give the third side as static input and store it in another variable.
  • Give the fourth side as static input and store it in another variable.
  • Calculate the s value using the above given mathematical formula and store it in another variable.
  • Calculate the maximum area of the given quadrilateral using the above given mathematical formula, math.sqrt() function and store it in another variable.
  • Print the maximum area of the given quadrilateral.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the first side as static input and store it in a variable.
p = 2
# Give the second side as static input and store it in another variable.
q = 1
# Give the third side as static input and store it in another variable.
r = 3
# Give the fourth side as static input and store it in another variable.
s = 4
# Calculate the s value using the above given mathematical formula and 
# store it in another variable.
s_valu = (p + q + r + s) / 2
# Calculate the maximum area of the given quadrilateral using the above
# given mathematical formula, math.sqrt() function and store it in another variable.
quadriltrlmax_area = math.sqrt(
    (s_valu - p) * (s_valu - q) * (s_valu - r) * (s_valu - s))
# Print the maximum area of the given quadrilateral.
print(
    "The maximimum area of quadrilateral for the given four sides {", p, ",", q, ",", r, ",", s, "} = ", quadriltrlmax_area)
# include <iostream>
# include<cmath>
using namespace std

int main() {
    int p = 2
    int q = 1
    int r = 3
    int s = 4
    int s_valu = (p + q + r + s) / 2
    double quadriltrlMaxArea = sqrt((s_valu - p) * (s_valu - q) * (s_valu - r) * (s_valu - s))
    cout << "The maximimum area of quadrilateral for the given four sides {" << p << "," << q << "," << r << "," << s << "} = " << quadriltrlMaxArea << endl
    return 0
}

Output:

The maximimum area of quadrilateral for the given four sides { 2 , 1 , 3 , 4 } =  4.898979485566356

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the first side as user input using the int(input()) function and store it in a variable.
  • Give the second side as user input using the int(input()) function and store it in another variable.
  • Give the third side as user input using the int(input()) function and store it in another variable.
  • Give the fourth side as user input using the int(input()) function and store it in another variable.
  • Calculate the s value using the above given mathematical formula and store it in another variable.
  • Calculate the maximum area of the given quadrilateral using the above given mathematical formula, math.sqrt() function and store it in another variable.
  • Print the maximum area of the given quadrilateral.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the first side as user input using the int(input()) function and store it in a variable.
p = int(input("Enter some random number = "))
# Give the second side as user input using the int(input()) function and store it in another variable.
q = int(input("Enter some random number = "))
# Give the third side as user input using the int(input()) function and store it in another variable.
r = int(input("Enter some random number = "))
# Give the fourth side as user input using the int(input()) function and store it in another variable.
s = int(input("Enter some random number = "))
# Calculate the s value using the above given mathematical formula and 
# store it in another variable.
s_valu = (p + q + r + s) / 2
# Calculate the maximum area of the given quadrilateral using the above
# given mathematical formula, math.sqrt() function and store it in another variable.
quadriltrlmax_area = math.sqrt(
    (s_valu - p) * (s_valu - q) * (s_valu - r) * (s_valu - s))
# Print the maximum area of the given quadrilateral.
print(
    "The maximimum area of quadrilateral for the given four sides {", p, ",", q, ",", r, ",", s, "} = ", quadriltrlmax_area)

Output:

Enter some random number = 5
Enter some random number = 3
Enter some random number = 5
Enter some random number = 2
The maximimum area of quadrilateral for the given four sides { 5 , 3 , 5 , 2 } = 12.43734296383275

Output:

Python Program for Maximum Area of Quadrilateral Read More »

Program for Pythagorean Quadruple

Python Program for Pythagorean Quadruple

Given four points, the task is to check if the given four points form quadruple in python.

Quadruple:

It is defined as a tuple of integers a, b, c, and d such that a2 + b2  + c2 = d2. They are, in fact, Diophantine Equations solutions. It represents a cuboid with integer side lengths |a|, |b|, and |c| and a space diagonal of |d| in the geometric interpretation.

Condition to check Quadruple = a2 + b2  + c2 = d2

where a, b, c, d are the given four points.

Examples:

Example1:

Input:

Given first point = 6
Given second point = 2
Given third point = 3
Given fourth point = 7

Output:

The given four points { 6 , 2 , 3 , 7 } forms a quadruple

Example2:

Input:

Given first point = 9
Given second point = 2
Given third point = 6 
Given fourth point = 11

Output:

The given four points { 9 , 2 , 6 , 11 } forms a quadruple

Program for Pythagorean Quadruple in Python

Below are the ways to check if the given four points form quadruple in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Give the third number as static input and store it in another variable.
  • Give the fourth number as static input and store it in another variable.
  • Calculate the sum of squares of the given three numbers using the above given mathematical formula and store it in another variable.
  • Check if the above result sum is equal to the square of the fourth number using the if conditional statement.
  • If it is true, then print “The given four points forms a quadruple”.
  • Else print “The given four points do not form a quadruple”.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as static input and store it in a variable.
p = 6
# Give the second number as static input and store it in another variable.
q = 2
# Give the third number as static input and store it in another variable.
r = 3
# Give the fourth number as static input and store it in another variable.
s = 7
# Calculate the sum of squares of the given three numbers using the above given
# mathematical formula and store it in another variable.
rslt_sum = p * p + q * q + r * r
# Check if the above result sum is equal to the square of the fourth number 
# using the if conditional statement.
if (s * s == rslt_sum):
    # If it is true, then print "The given four points forms a quadruple".
    print("The given four points {", p, ",", q,
          ",", r, ",", s, "} forms a quadruple")
# Else print "The given four points do not form a quadruple".
else:
    print("The given four points {", p, ",", q,
          ",", r, ",", s, "} do not form a quadruple")
# include <iostream>

using namespace std

int main() {
    double p = 6
    double q = 12
    double r = 3
    double s = 7
    double rslt_sum = p * p + q * q + r * r
    if ((s * s == rslt_sum)) {
        cout << "The given four points {" << p << "," << q << "," << r << "," << s << "} forms a quadruple" << endl
    }
    else {
        cout << "The given four points {" << p << "," << q << "," << r << "," << s << "} do not form a quadruple" << endl
    }

}

Output:

The given four points { 6 , 2 , 3 , 7 } forms a quadruple

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second 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.
  • Give the fourth number as user input using the int(input()) function and store it in another variable.
  • Calculate the sum of squares of the given three numbers using the above given mathematical formula and store it in another variable.
  • Check if the above result sum is equal to the square of the fourth number using the if conditional statement.
  • If it is true, then print “The given four points forms a quadruple”.
  • Else print “The given four points do not form a quadruple”.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as user input using the int(input()) function and store it in a variable.
p = int(input("Enter some random number = "))
# Give the second 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 = "))
# Give the fourth number as user input using the int(input()) function and store it in another variable.
s = int(input("Enter some random number = "))
# Calculate the sum of squares of the given first three numbers using the above given
# mathematical formula and store it in another variable.
rslt_sum = p * p + q * q + r * r
# Check if the above result sum is equal to the square of the fourth number
# using the if conditional statement.
if (s * s == rslt_sum):
    # If it is true, then print "The given four points forms a quadruple".
    print("The given four points {", p, ",", q,
          ",", r, ",", s, "} forms a quadruple")
# Else print "The given four points do not form a quadruple".
else:
    print("The given four points {", p, ",", q,
          ",", r, ",", s, "} do not form a quadruple")

Output:

Enter some random number = 9
Enter some random number = 2
Enter some random number = 6
Enter some random number = 11
The given four points { 9 , 2 , 6 , 11 } forms a quadruple

Python Program for Pythagorean Quadruple Read More »

Program to Calculate Area and Volume of a Tetrahedron

Python Program to Calculate Area and Volume of a Tetrahedron

Tetrahedron:

A Tetrahedron is nothing more than a pyramid with a triangular base. It has four triangular faces, three on the sides or lateral faces, one on the bottom or base, and four vertices or corners. If all of the faces are congruent equilateral triangles, the tetrahedron is said to be regular.

Formula:

Area of Tetrahedron =sqrt(3)*side*side

Volume = (side ** 3 / (6 * math.sqrt(2)))

Given the side of a tetrahedron and the task is to calculate the area and volume of the given tetrahedron in python.

Examples:

Example1:

Input:

Given side = 5

Output:

The Area of Tetrahedron with the given side { 5 } = 43.301
The volume of Tetrahedron with the given side { 5 } = 14.731

Example2:

Input:

Given side = 10

Output:

The Area of Tetrahedron with the given side { 10 } =  173.205
The volume of Tetrahedron with the given side { 10 } =  117.851

Program to Calculate Area and Volume of a Tetrahedron in Python

Below are the ways to calculate the area and volume of the given tetrahedron in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the side as static input and store it in a variable.
  • Create a function to say TetrahedronArea() which takes the given side as an argument and returns the area of the given tetrahedron.
  • Inside the function, calculate the area of a given tetrahedron using the above given mathematical formula and math.sqrt() function.
  • Store it in a variable.
  • Return the above result.
  • Create another function to say Tetrahedronvolume() which takes the given side as an argument and returns the volume of the given tetrahedron.
  • Inside the function, calculate the volume of a given tetrahedron using the above given mathematical formula and math.sqrt() function.
  • Store it in another variable.
  • Return the above result.
  • Pass the given side as an argument to the TetrahedronArea() function and round it off to up to 3 places after the decimal point using the round() function.
  • Store it in another variable.
  • Print the above result which is the area of the given tetrahedron.
  • Pass the given side as an argument to the Tetrahedronvolume() function and round it off to up to 3 places after the decimal point using the round() function.
  • Store it in another variable.
  • Print the above result which is the volume of the given tetrahedron.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math

# Create a function to say TetrahedronArea() which takes the given side as an argument
# and returns the area of the given tetrahedron.


def TetrahedronArea(gvn_side):
    # Inside the function, calculate the area of a given tetrahedron using the above
    # given mathematical formula and math.sqrt() function.
    # Store it in a variable.
    rslt_area = math.sqrt(3) * (gvn_side * gvn_side)
    # Return the above result.
    return rslt_area

# Create another function to say Tetrahedronvolume() which takes the given side as an
# argument and returns the volume of the given tetrahedron.


def Tetrahedronvolume(gvn_side):
    # Inside the function, calculate the volume of a given tetrahedron using the above
    # given mathematical formula and math.sqrt() function.
    # Store it in a variable.
    rslt_vol = (gvn_side ** 3 / (6 * math.sqrt(2)))
    # Return the above result.
    return rslt_vol


# Give the side as static input and store it in a variable.
gvn_side = 10
# Pass the given side as an argument to the TetrahedronArea() function and round it
# off to up to 3 places after the decimal point using the round() function.
# Store it in another variable.
area_tetrahdrn = round(TetrahedronArea(gvn_side), 3)
# Print the above result which is the area of the given tetrahedron.
print(
    "The Area of Tetrahedron with the given side {", gvn_side, "} = ", area_tetrahdrn)
# Pass the given side as an argument to the Tetrahedronvolume() function and round it off
# to up to 3 places after the decimal point using the round() function.
# Store it in another variable.
volum_tetrahdrn = round(Tetrahedronvolume(gvn_side), 3)
# Print the above result which is the volume of the given tetrahedron.
print(
    "The volume of Tetrahedron with the given side {", gvn_side, "} = ", volum_tetrahdrn)
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
    int gvn_side = 10;
    double area_tetrahdrn = sqrt(3) * (gvn_side * gvn_side);
    double volum_tetrahdrn
        = (pow(gvn_side, 3) / (6 * sqrt(2)));
    cout << "The Area of Tetrahedron with the given side {"
         << gvn_side << "} = " << area_tetrahdrn << endl;
    cout
        << "The Volume of Tetrahedron with the given side {"
        << gvn_side << "} = " << volum_tetrahdrn << endl;
    return 0;
}

Output:

The Area of Tetrahedron with the given side { 10 } =  173.205
The volume of Tetrahedron with the given side { 10 } =  117.851

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the side as user input using the int(input()) function and store it in a variable.
  • Create a function to say TetrahedronArea() which takes the given side as an argument and returns the area of the given tetrahedron.
  • Inside the function, calculate the area of a given tetrahedron using the above given mathematical formula and math.sqrt() function.
  • Store it in a variable.
  • Return the above result.
  • Create another function to say Tetrahedronvolume() which takes the given side as an argument and returns the volume of the given tetrahedron.
  • Inside the function, calculate the volume of a given tetrahedron using the above given mathematical formula and math.sqrt() function.
  • Store it in another variable.
  • Return the above result.
  • Pass the given side as an argument to the TetrahedronArea() function and round it off to up to 3 places after the decimal point using the round() function.
  • Store it in another variable.
  • Print the above result which is the area of the given tetrahedron.
  • Pass the given side as an argument to the Tetrahedronvolume() function and round it off to up to 3 places after the decimal point using the round() function.
  • Store it in another variable.
  • Print the above result which is the volume of the given tetrahedron.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math

# Create a function to say TetrahedronArea() which takes the given side as an argument
# and returns the area of the given tetrahedron.


def TetrahedronArea(gvn_side):
    # Inside the function, calculate the area of a given tetrahedron using the above
    # given mathematical formula and math.sqrt() function.
    # Store it in a variable.
    rslt_area = math.sqrt(3) * (gvn_side * gvn_side)
    # Return the above result.
    return rslt_area

# Create another function to say Tetrahedronvolume() which takes the given side as an
# argument and returns the volume of the given tetrahedron.


def Tetrahedronvolume(gvn_side):
    # Inside the function, calculate the volume of a given tetrahedron using the above
    # given mathematical formula and math.sqrt() function.
    # Store it in a variable.
    rslt_vol = (gvn_side ** 3 / (6 * math.sqrt(2)))
    # Return the above result.
    return rslt_vol


# Give the side as user input using the int(input()) function and store it in a variable.
gvn_side =  int(input("Enter some random number = "))
# Pass the given side as an argument to the TetrahedronArea() function and round it
# off to up to 3 places after the decimal point using the round() function.
# Store it in another variable.
area_tetrahdrn = round(TetrahedronArea(gvn_side), 3)
# Print the above result which is the area of the given tetrahedron.
print(
    "The Area of Tetrahedron with the given side {", gvn_side, "} = ", area_tetrahdrn)
# Pass the given side as an argument to the Tetrahedronvolume() function and round it off
# to up to 3 places after the decimal point using the round() function.
# Store it in another variable.
volum_tetrahdrn = round(Tetrahedronvolume(gvn_side), 3)
# Print the above result which is the volume of the given tetrahedron.
print(
    "The volume of Tetrahedron with the given side {", gvn_side, "} = ", volum_tetrahdrn)

Output:

Enter some random number = 5
The Area of Tetrahedron with the given side { 5 } = 43.301
The volume of Tetrahedron with the given side { 5 } = 14.731

Python Program to Calculate Area and Volume of a Tetrahedron Read More »

Program for Circumference of a Parallelogram

Python Program for Circumference of a Parallelogram

Given the sides of a parallelogram, the task is to calculate the circumference of a parallelogram for the given sides in python.

Circumference of Parallelogram:

A parallelogram’s opposite sides are equal in length and parallel. Angles are equal when paired, but not always at 90 degrees. The circumference of a parallelogram can be calculated by multiplying the sum of two adjacent sides by two.

Formula:

The circumference of parallelogram = (2*a)+(2*b)

where a, b are the sides of a parallelogram

Python round() function:

The round() function returns a floating-point number with the specified number of decimals that is a rounded version of the specified number.

Examples:

Example1:

Input:

Given first side = 5
Given second side = 6

Output:

The Circumference of a parallelogram for the given two sides{ 5 , 6 } =  22

Example2:

Input:

Given first side = 3.85
Given second side = 8.64

Output:

The Circumference of a parallelogram for the given two sides{ 3.85 , 8.64 } = 24.98

Program for Circumference of a Parallelogram in Python

Below are the ways to calculate the circumference of a parallelogram for the given two sides in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the first side as static input and store it in a variable.
  • Give the second side as static input and store it in another variable.
  • Create a function to say parallelogram_circumference() which takes the given two sides as the argument and returns the circumference of the given parallelogram.
  • Inside the function, calculate the circumference of the parallelogram using the above given mathematical formula and store it in another variable.
  • Return the above result.
  • Pass the given two sides as the arguments to the parallelogram_circumference() function and round it off to up to 3 places after the decimal point using the round() function.
  • Store it in another variable.
  • Print the above result which gives the circumference of the parallelogram for the given sides.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say parallelogram_circumference() which takes the given two
# sides as the argument and returns the circumference of the given parallelogram.


def parallelogram_circumference(fst_side, scnd_side):
    # Inside the function, calculate the circumference of the parallelogram using the
        # above given mathematical formula and store it in another variable.
    paralgrm_circum = (2 * fst_side) + (2 * scnd_side)
    # Return the above result.
    return paralgrm_circum


# Give the first side as static input and store it in a variable.
fst_side = 5
# Give the second side as static input and store it in another variable.
scnd_side = 6
# Pass the given two sides as the arguments to the parallelogram_circumference() function
# and round it off to up to 3 places after the decimal point using the round() function.
# Store it in another variable.
circumfrnce = round(parallelogram_circumference(fst_side, scnd_side), 3)
# Print the above result which gives the circumference of the parallelogram for
# the given sides.
print(
    "The Circumference of a parallelogram for the given two sides{", fst_side, ",", scnd_side, "} = ", circumfrnce)
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
    int fst_side = 5;
    int scnd_side = 6;
    int paralgrm_circum = (2 * fst_side) + (2 * scnd_side);
    cout << "The Circumference of a parallelogram for the "
            "given two sides{"
         << fst_side << "," << scnd_side
         << "} = " << paralgrm_circum << endl;
    return 0;
}

Output:

The Circumference of a parallelogram for the given two sides{ 5 , 6 } =  22

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the first side as user input using the float(input()) function and store it in a variable.
  • Give the second side as user input using the float(input()) function and store it in another variable.
  • Create a function to say parallelogram_circumference() which takes the given two sides as the argument and returns the circumference of the given parallelogram.
  • Inside the function, calculate the circumference of the parallelogram using the above given mathematical formula and store it in another variable.
  • Return the above result.
  • Pass the given two sides as the arguments to the parallelogram_circumference() function and round it off to up to 3 places after the decimal point using the round() function.
  • Store it in another variable.
  • Print the above result which gives the circumference of the parallelogram for the given sides.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say parallelogram_circumference() which takes the given two
# sides as the argument and returns the circumference of the given parallelogram.


def parallelogram_circumference(fst_side, scnd_side):
    # Inside the function, calculate the circumference of the parallelogram using the
        # above given mathematical formula and store it in another variable.
    paralgrm_circum = (2 * fst_side) + (2 * scnd_side)
    # Return the above result.
    return paralgrm_circum


# Give the first side as user input using the float(input()) function and store it in a variable.
fst_side = float(input("Enter some random number = "))
# Give the second side as user input using the float(input()) function and 
# store it in another variable.
scnd_side = float(input("Enter some random number = "))
# Pass the given two sides as the arguments to the parallelogram_circumference() function
# and round it off to up to 3 places after the decimal point using the round() function.
# Store it in another variable.
circumfrnce = round(parallelogram_circumference(fst_side, scnd_side), 3)
# Print the above result which gives the circumference of the parallelogram for
# the given sides.
print(
    "The Circumference of a parallelogram for the given two sides{", fst_side, ",", scnd_side, "} = ", circumfrnce)

Output:

Enter some random number = 3.85
Enter some random number = 8.64
The Circumference of a parallelogram for the given two sides{ 3.85 , 8.64 } = 24.98

 

Python Program for Circumference of a Parallelogram Read More »

Program To Write Inline If Statement

Python Program To Write Inline If Statement

Python’s if-else statement is logical. Python does not use the ternary operator as other languages do for inline if statements. It instead provides a one-line code to evaluate the first expression if the condition is met. Otherwise, the second expression is evaluated.

Program To Write Inline If Statement in Python

inline if statement in Python:

In Python, the inline if-else expression contains two statements and executes one of them based on the condition’s evaluation. In Python, there are two kinds of if statements.

  1. Simple if statement
  2. if-else expression statement

In a simple, if statement, we write a condition, and the if block is executed based on that condition.

Syntax:

if condition: statement 
if condition:
    block

Let us write an inline if statement that will execute the expression if the condition is True.

Examples:

Example1:

numb = 12
if numb > 10: print("hello btechgeeks")

Output:

hello btechgeeks

Explanation:

Here the given number 12 is greater than 10. So, it prints the given statement "hello btechgeeks".

Example2:

numb = 32
if numb % 2 == 0 : print("The Given number is Even")

Output:

The Given number is Even

Explanation:

Here the given number 32 is Even. So, it prints the given statement "The Given number is Even".

inline if-else statement in python:

Python’s inline if-else expression must include an else clause.

Examples:

Example1:

numb = 10
temp = 3
print(numb if temp else 0)

Output:

10

Explanation:

Here it prints the given number value because the given if condition is True 
( temp is not equal to 0 so, it returns True)

Example2:

numb = 15
temp = 0
print(numb if temp else 0)

Output:

0

Explanation:

Here it prints 0 because the given if condition is False (given temp is 0)
suppose the temp given is any other number other than 0, then it prints the given number

User Input:

Example1:

numb = int(input("Enter some random number = ")) 
temp = 3 
print(numb if temp else 0)

Output:

Enter some random number = 10
10

Example 2:

numb = int(input("Enter some random number = ")) 
print("Even" if numb%2==0 else "Odd")

Output:

Enter some random number = 13
Odd

Example 3:

gvn_str = input("Enter some random String = ") 
print("True" if gvn_str=="hello btechgeeks" else "False")

Output:

Enter some random String = hello btechgeeks
True

Example 4:

gvn_str = input("Enter some random String = ")
print("True" if gvn_str=="hello btechgeeks" else "False")

Output:

Enter some random String = hello world
False

Python Program To Write Inline If Statement Read More »

Program for Volume and Surface Area of Octahedron

Python Program for Volume and Surface Area of Octahedron

Given the side of an octahedron and the task is to calculate the volume and surface area of an octahedron for the given side in python.

Octahedron :

A regular octahedron has eight faces, all of which are equilateral triangles. An octahedron’s area is equal to 2 multiplied by the length of an edge squared multiplied by the square root of three.

Formulas:

Surface Area of an Octahedron = 2*(sqrt(3))*(side*side)

The volume of an Octahedron = √2/3 × side3

Examples:

Example1:

Input:

Given side = 5

Output:

The Surface Area of an octahedron for the given side { 5 } =  86.60254037844386
The volume of an Octahedron for the given side { 5 } =  58.92556509887897

Example2:

Input:

Given side = 10

Output:

The Surface Area of an octahedron for the given side { 10 } =  346.41016151377545
The volume of an Octahedron for the given side { 10 } =  471.40452079103176

Program for Volume and Surface Area of Octahedron in Python

Below are the ways to calculate the volume and surface area of an octahedron for the given side in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

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

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the side as static input and store it in a variable.
gvn_side = 5
# Calculate the surface area of an octahedron using the above given mathematical
# formula and math.sqrt() function.
# Store it in another variable.
surfce_area = (2*(math.sqrt(3))*(gvn_side*gvn_side))
# Calculate the volume of an octahedron using the above given mathematical formula
# and math.sqrt() function.
# Store it in another variable.
rslt_volum = ((gvn_side*gvn_side*gvn_side)*(math.sqrt(2)/3))
# Print the surface area of a given octahedron.
print(
    "The Surface Area of an octahedron for the given side {", gvn_side, "} = ", surfce_area)
# Print the volume of a given octahedron.
print(
    "The volume of an Octahedron for the given side {", gvn_side, "} = ", rslt_volum)
#include <iostream>
#include<math.h>

using namespace std;

int main() {
   double gvn_side = 5;
  double surfce_area = ( 2 * ( sqrt ( 3 ) ) * ( gvn_side * gvn_side ) );
  double rslt_volum = ( ( gvn_side * gvn_side * gvn_side ) * ( sqrt ( 2 ) / 3 ) );
  cout << "The Surface Area of an octahedron for the given side {" << gvn_side << "} = " << surfce_area << endl;
  cout << "The volume of an Octahedron for the given side {" << gvn_side << "} = " << rslt_volum << endl;

}

Output:

The Surface Area of an octahedron for the given side { 5 } =  86.60254037844386
The volume of an Octahedron for the given side { 5 } =  58.92556509887897

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the side as user input using the int(input()) function and store it in a variable.
  • Calculate the surface area of an octahedron using the above given mathematical formula and math.sqrt() function.
  • Store it in another variable.
  • Calculate the volume of an octahedron using the above given mathematical formula and math.sqrt() function.
  • Store it in another variable.
  • Print the surface area of a given octahedron.
  • Print the volume of a given octahedron.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the side as user input using the int(input()) function and store it in a variable.
gvn_side = int(input("Enter some random number = "))
# Calculate the surface area of an octahedron using the above given mathematical
# formula and math.sqrt() function.
# Store it in another variable.
surfce_area = (2*(math.sqrt(3))*(gvn_side*gvn_side))
# Calculate the volume of an octahedron using the above given mathematical formula
# and math.sqrt() function.
# Store it in another variable.
rslt_volum = ((gvn_side*gvn_side*gvn_side)*(math.sqrt(2)/3))
# Print the surface area of a given octahedron.
print(
    "The Surface Area of an octahedron for the given side {", gvn_side, "} = ", surfce_area)
# Print the volume of a given octahedron.
print(
    "The volume of an Octahedron for the given side {", gvn_side, "} = ", rslt_volum)

Output:

Enter some random number = 10
The Surface Area of an octahedron for the given side { 10 } = 346.41016151377545
The volume of an Octahedron for the given side { 10 } = 471.40452079103176

 

 

Python Program for Volume and Surface Area of Octahedron Read More »

Program to Calculate Volume of Ellipsoid

Python Program to Calculate Volume of Ellipsoid

Given the three radius values of an ellipsoid and the task is to calculate the volume of a given ellipsoid in python.

Ellipsoid :

Ellipsoid is a closed surface whose plane cross-sections are all ellipses or circles. An ellipsoid has three mutually perpendicular axes that intersect at the center. It is a three-dimensional, closed geometric shape with all of its planar sections being ellipses or circles.

An ellipsoid has three independent axes and is usually defined by the lengths of the three semi-axes, a, b, and c. If an ellipsoid is formed by rotating an ellipse about one of its axes, the ellipsoid’s two axes are the same, and it is known as an ellipsoid of revolution or spheroid. It is a sphere if the lengths of all three of its axes are the same.

Formula:

Volume = (4/3) * pi * r1 * r2 * r3

where pi = 3.1415…

r1 = first radius

r2 = second radius

r3 =  third radius

Examples:

Example1:

Input:

Given first radius = 1
Given second radius = 3
Given third radius = 6.5

Output:

The Volume of given ellipsoid with given radii { 1 3 6.5 } =  81.4772054708513

Example2:

Input:

Given first radius = 2
Given second radius = 4.5
Given third radius = 8

Output:

The Volume of given ellipsoid with given radii { 2 4.5 8 } =  300.8389125077586

Program to Calculate Volume of Ellipsoid in Python

Below are the ways to calculate the volume of a given ellipsoid in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the first radius as static input and store it in a variable.
  • Give the second radius as static input and store it in another variable.
  • Give the third radius as static input and store it in another variable.
  • Create a function to say Ellipsoid_volume() which takes the given three radii as the arguments and returns the volume of a given ellipsoid.
  • Inside the function, calculate the volume of the ellipsoid using the above given mathematical formula, math. pi and store it in a variable.
  • Return the above result.
  • Pass the given three radii as the arguments to the Ellipsoid_volume() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math

# Create a function to say Ellipsoid_volume() which takes the given three radii as
# the arguments and returns the volume of a given ellipsoid.


def Ellipsoid_volume(fst_radiuss, scnd_radiuss, thrd_radiuss):
    # Inside the function, calculate the volume of the ellipsoid using the above given
    # mathematical formula, math.pi and store it in a variable.
    rslt_vol = 1.33 * math.pi * fst_radiuss * scnd_radiuss * thrd_radiuss
    # Return the above result.
    return rslt_vol


# Give the first radius as static input and store it in a variable.
fst_radiuss = 1
# Give the second radius as static input and store it in another variable.
scnd_radiuss = 3
# Give the third radius as static input and store it in another variable.
thrd_radiuss = 6.5
# Pass the given three radii as the arguments to the Ellipsoid_volume() function
# and print it.
print("The Volume of given ellipsoid with given radii {", fst_radiuss, scnd_radiuss, thrd_radiuss, "} = ",
      Ellipsoid_volume(fst_radiuss, scnd_radiuss, thrd_radiuss))
#include <iostream>
#include <cmath>

using namespace std;

int main() {
  double fst_radiuss = 1;
   double scnd_radiuss = 3;
   double thrd_radiuss = 6.5;
   double rslt_vol = 1.33 * M_PI * fst_radiuss * scnd_radiuss * thrd_radiuss;
 cout << "The Volume of given ellipsoid with given radii {" << fst_radiuss << ", " << scnd_radiuss << ", " << thrd_radiuss << "} = " << rslt_vol << rslt_vol;

}

Output:

The Volume of given ellipsoid with given radii { 1 3 6.5 } =  81.4772054708513

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the first radius as user input using the float(input()) function and store it in a variable.
  • Give the second radius as user input using the float(input()) function and store it in another variable.
  • Give the third radius as user input using the float(input()) function and store it in another variable.
  • Create a function to say Ellipsoid_volume() which takes the given three radii as the arguments and returns the volume of a given ellipsoid.
  • Inside the function, calculate the volume of the ellipsoid using the above given mathematical formula, math. pi and store it in a variable.
  • Return the above result.
  • Pass the given three radii as the arguments to the Ellipsoid_volume() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math

# Create a function to say Ellipsoid_volume() which takes the given three radii as
# the arguments and returns the volume of a given ellipsoid.


def Ellipsoid_volume(fst_radiuss, scnd_radiuss, thrd_radiuss):
    # Inside the function, calculate the volume of the ellipsoid using the above given
    # mathematical formula, math.pi and store it in a variable.
    rslt_vol = 1.33 * math.pi * fst_radiuss * scnd_radiuss * thrd_radiuss
    # Return the above result.
    return rslt_vol


# Give the first radius as user input using the float(input()) function and 
# store it in a variable.
fst_radiuss = float(input("Enter some random number = "))
# Give the second radius as user input using the float(input()) function and 
# store it in another variable.
scnd_radiuss = float(input("Enter some random number = "))
# Give the third radius as user input using the float(input()) function and 
# store it in another variable.
thrd_radiuss = float(input("Enter some random number = "))
# Pass the given three radii as the arguments to the Ellipsoid_volume() function
# and print it.
print("The Volume of given ellipsoid with given radii {", fst_radiuss, scnd_radiuss, thrd_radiuss, "} = ",
      Ellipsoid_volume(fst_radiuss, scnd_radiuss, thrd_radiuss))

Output:

Enter some random number = 2
Enter some random number = 4.5
Enter some random number = 8
The Volume of given ellipsoid with given radii { 2.0 4.5 8.0 } = 300.8389125077586

 

Python Program to Calculate Volume of Ellipsoid Read More »

Program for Maximize Volume of Cuboid with Given Sum of Sides

Python Program for Maximize Volume of Cuboid with Given Sum of Sides

Given the sum of length, breadth, and height of a cuboid say S and the task is to get the maximum volume of a cuboid such that the sum of the side is S.

Examples:

Example1:

Input:

Given sum = 5

Output:

The maximum volume of a cuboid such that the sum of the side { 5 } =  4

Example2:

Input:

Given sum = 11

Output:

The maximum volume of a cuboid such that the sum of the side { 11 } = 48

Program for Maximize Volume of Cuboid with Given Sum of Sides in Python

Below are the ways to get the maximum volume of a cuboid such that the sum of the side is S in python:

Method #1: Using For loop (Static Input)

Approach:

  • Give the number (sum) as static input and store it in a variable.
  • Create a function to say Maximum_volume() which takes the given number as an argument and returns the maximum volume of a cuboid such that the sum of the side is the given sum.
  • Inside the function, take a variable to say maxim_vol and initialize its value to 0.
  • Take another variable (for length)  say a and initialize its value to 1.
  • Loop till the given sum -1 using the for loop.
  • Take another variable (for breadth) say b and initialize its value to 1.
  • Loop till the given sum using another nested for loop.
  • Subtract the above variables a, b values from the given sum and store it in a variable say c (for height).
  • Multiply the above a, b, c values and store them in another variable.
  • Get the maximum value from the above-initialized maxim_vol and above multiplication result using the max() function and store it in another variable.
  • Return the above result which is the maximum volume of a cuboid.
  • Pass the given number as an argument to the Maximum_volume() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say Maximum_volume() which takes the given number as an
# argument and returns the maximum volume of a cuboid such that the sum of the side
# is the given sum.



def Maximum_volume(gvn_sum):
  # Inside the function, take a variable and initialize its value to 0.
    maxim_vol = 0

    # Take another variable (for length) say a and initialize its value to 1.
    a = 1
    # Loop till the given sum -1 using the for loop.
    for a in range(gvn_sum - 1):
      # Take another variable (for breadth) say b and initialize its value to 1.
        b = 1

        # Loop till the given sum using the another nested for loop.
        for b in range(gvn_sum):
            # Subtract the above variables a, b values from the given sum and store
            # it in a variable say c.
            c = gvn_sum - a - b
            # Multiply the above a, b, c values and store them in another variable.

            mult = a * b * c
            # Get the maximum value from the above-initialized maxim_vol and above
            # multiplication result using the max() function and store it in
            # another variable.
            maxim_vol = max(maxim_vol, mult)
        # Return the above result which is the maximum volume of a cuboid.
    return maxim_vol


# Give the number (sum) as static input and store it in a variable.
gvn_sum = 5
# Pass the given number as an argument to the Maximum_volume() function
# and print it.
print("The maximum volume of a cuboid such that the sum of the side {", gvn_sum, "} = ", Maximum_volume(
    gvn_sum))

Output:

The maximum volume of a cuboid such that the sum of the side { 5 } =  4

Method #2: Using For loop (User Input)

Approach:

  • Give the number (sum) as user input using the int(input()) function and store it in a variable.
  • Create a function to say Maximum_volume() which takes the given number as an argument and returns the maximum volume of a cuboid such that the sum of the side is the given sum.
  • Inside the function, take a variable to say maxim_vol and initialize its value to 0.
  • Take another variable (for length)  say a and initialize its value to 1.
  • Loop till the given sum -1 using the for loop.
  • Take another variable (for breadth) say b and initialize its value to 1.
  • Loop till the given sum using another nested for loop.
  • Subtract the above variables a, b values from the given sum and store it in a variable say c (for height).
  • Multiply the above a, b, c values and store them in another variable.
  • Get the maximum value from the above-initialized maxim_vol and above multiplication result using the max() function and store it in another variable.
  • Return the above result which is the maximum volume of a cuboid.
  • Pass the given number as an argument to the Maximum_volume() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say Maximum_volume() which takes the given number as an
# argument and returns the maximum volume of a cuboid such that the sum of the side
# is the given sum.

def Maximum_volume(gvn_sum):
  # Inside the function, take a variable and initialize its value to 0.
    maxim_vol = 0

    # Take another variable (for length) say a and initialize its value to 1.
    a = 1
    # Loop till the given sum -1 using the for loop.
    for a in range(gvn_sum - 1):
      # Take another variable (for breadth) say b and initialize its value to 1.
        b = 1

        # Loop till the given sum using the another nested for loop.
        for b in range(gvn_sum):
            # Subtract the above variables a, b values from the given sum and store
            # it in a variable say c.(for height)
            c = gvn_sum - a - b
            # Multiply the above a, b, c values and store them in another variable.

            mult = a * b * c
            # Get the maximum value from the above-initialized maxim_vol and above
            # multiplication result using the max() function and store it in
            # another variable.
            maxim_vol = max(maxim_vol, mult)
        # Return the above result which is the maximum volume of a cuboid.
    return maxim_vol


# Give the number (sum) as user input using the int(input()) function and 
# store it in a variable.
gvn_sum = int(input("Enter some random number = "))
# Pass the given number as an argument to the Maximum_volume() function
# and print it.
print("The maximum volume of a cuboid such that the sum of the side {", gvn_sum, "} = ", Maximum_volume(
    gvn_sum))

Output:

Enter some random number = 11
The maximum volume of a cuboid such that the sum of the side { 11 } = 48

Python Program for Maximize Volume of Cuboid with Given Sum of Sides Read More »

Program for Volume of Pyramid

Python Program for Volume of Pyramid

Given the base, height, area of a pyramid and the task is to calculate the volume of given pyramid types in python.

Pyramid :

A pyramid is a three-dimensional geometric shape created by connecting all of the corners of a polygon to a central apex.

Pyramids come in a variety of shapes and sizes. They are usually named after the type of base they have. Let’s take a look at some of the most common types of pyramids below.

Formulas:

The Volume of square base pyramid  = (1/3) * (b^2) * h
The volume of triangular base pyramid = (1/6) * a * b * h
The Volume of pentagonal base pyramid = (5/6) * a * b * h
The volume of a hexagonal base pyramid = a * b * h

where b= base of the pyramid

h= height

a= area

Examples:

Example1:

Input:

Given base = 3
Given height = 5
Given area = 20

Output:

The Volume of triangular base pyramid with given base, height, area { 3 , 5 , 20 } =  49.97999999999999
The Volume of square base pyramid with given base, height, area { 3 , 5 , 20 } =  14.849999999999998
The Volume of pentagonal base pyramid with given base, height, area { 3 , 5 , 20 } =  249.0
The Volume of hexagonal base pyramid with given base, height, area { 3 , 5 , 20 } =  300

Example2:

Input:

Given base = 4
Given height = 6.2
Given area =  15

Output:

The Volume of triangular base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 61.9752
The Volume of square base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 32.736000000000004
The Volume of pentagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 308.76
The Volume of hexagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 372.0

Program for Volume of Pyramid in Python

Below are the ways to calculate the volume of given pyramid types in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the base as static input and store it in a variable.
  • Give the height as static input and store it in another variable.
  • Give the area as static input and store it in another variable.
  • Calculate the volume of the triangular pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the square pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the pentagonal pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the hexagonal pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Print the volume of the triangular pyramid.
  • Print the volume of the square pyramid.
  • Print the volume of the pentagonal pyramid.
  • Print the volume of the hexagonal pyramid.
  • The Exit of the Program.

Below is the implementation:

# Give the base as static input and store it in a variable.
gvn_baseval = 3
# Give the height as static input and store it in another variable.
gvn_heigt = 5
# Give the area as static input and store it in another variable.
gvn_area = 20
# Calculate the volume of the triangular pyramid using the above given mathematical formula.
# Store it in another variable.
triangulr_pyramidvol = (0.1666) * gvn_area * gvn_baseval * gvn_heigt
# Calculate the volume of the square pyramid using the above given mathematical formula.
# Store it in another variable.
squre_pyramidvol = (0.33) * gvn_baseval * gvn_baseval * gvn_heigt
# Calculate the volume of the pentagonal pyramid using the above given mathematical formula.
# Store it in another variable.
pentagonl_pyramidvol = (0.83) * gvn_area * gvn_baseval * gvn_heigt
# Calculate the volume of the hexagonal pyramid using the above given mathematical formula.
# Store it in another variable.
hexgnl_pyramidvol = gvn_area * gvn_baseval * gvn_heigt
# Print the volume of the triangular pyramid.
print("The Volume of triangular base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", triangulr_pyramidvol)
# Print the volume of the square pyramid.
print("The Volume of square base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", squre_pyramidvol)
# Print the volume of the pentagonal pyramid.
print("The Volume of pentagonal base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", pentagonl_pyramidvol)
# Print the volume of the hexagonal pyramid.
print("The Volume of hexagonal base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", hexgnl_pyramidvol)
#include <iostream>
#include<math.h>

using namespace std;

int main() {
 int gvn_baseval = 3;
int gvn_heigt = 5;
int gvn_area = 20;
double triangulr_pyramidvol = ( 0.1666 ) * gvn_area * gvn_baseval * gvn_heigt;
double squre_pyramidvol = ( 0.33 ) * gvn_baseval * gvn_baseval * gvn_heigt;
double pentagonl_pyramidvol = ( 0.83 ) * gvn_area * gvn_baseval * gvn_heigt;
 double hexgnl_pyramidvol = gvn_area * gvn_baseval * gvn_heigt;
cout<<"The Volume of triangular base pyramid with given base, height, area {"<<gvn_baseval<< ","<<gvn_heigt<<","<<gvn_area<<"} = "<<triangulr_pyramidvol<<endl;
cout<<"The Volume of square  base pyramid with given base, height, area {"<<gvn_baseval<< ","<<gvn_heigt<<","<<gvn_area<<"} = "<<squre_pyramidvol<<endl;
cout<<"The Volume of pentagonal  base pyramid with given base, height, area {"<<gvn_baseval<< ","<<gvn_heigt<<","<<gvn_area<<"} = "<<pentagonl_pyramidvol<<endl;
cout<<"The Volume of hexagonal base pyramid with given base, height, area {"<<gvn_baseval<< ","<<gvn_heigt<<","<<gvn_area<<"} = "<<hexgnl_pyramidvol<<endl;

}

Output:

The Volume of triangular base pyramid with given base, height, area { 3 , 5 , 20 } =  49.97999999999999
The Volume of square base pyramid with given base, height, area { 3 , 5 , 20 } =  14.849999999999998
The Volume of pentagonal base pyramid with given base, height, area { 3 , 5 , 20 } =  249.0
The Volume of hexagonal base pyramid with given base, height, area { 3 , 5 , 20 } =  300

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the base as user input using the float(input()) function and store it in a variable.
  • Give the height as user input using the float(input()) function and store it in another variable.
  • Give the area as user input using the float(input()) function and store it in another variable.
  • Calculate the volume of the triangular pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the square pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the pentagonal pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the hexagonal pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Print the volume of the triangular pyramid.
  • Print the volume of the square pyramid.
  • Print the volume of the pentagonal pyramid.
  • Print the volume of the hexagonal pyramid.
  • The Exit of the Program.

Below is the implementation:

# Give the base as user input using the float(input()) function and store it in a variable.
gvn_baseval = float(input("Enter some random number = "))
# Give the height as user input using the float(input()) function and store it in another variable.
gvn_heigt = float(input("Enter some random number = "))
# Give the area as user input using the float(input()) function and store it in another variable.
gvn_area = float(input("Enter some random number = "))
# Calculate the volume of the triangular pyramid using the above given mathematical formula.
# Store it in another variable.
triangulr_pyramidvol = (0.1666) * gvn_area * gvn_baseval * gvn_heigt
# Calculate the volume of the square pyramid using the above given mathematical formula.
# Store it in another variable.
squre_pyramidvol = (0.33) * gvn_baseval * gvn_baseval * gvn_heigt
# Calculate the volume of the pentagonal pyramid using the above given mathematical formula.
# Store it in another variable.
pentagonl_pyramidvol = (0.83) * gvn_area * gvn_baseval * gvn_heigt
# Calculate the volume of the hexagonal pyramid using the above given mathematical formula.
# Store it in another variable.
hexgnl_pyramidvol = gvn_area * gvn_baseval * gvn_heigt
# Print the volume of the triangular pyramid.
print("The Volume of triangular base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", triangulr_pyramidvol)
# Print the volume of the square pyramid.
print("The Volume of square base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", squre_pyramidvol)
# Print the volume of the pentagonal pyramid.
print("The Volume of pentagonal base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", pentagonl_pyramidvol)
# Print the volume of the hexagonal pyramid.
print("The Volume of hexagonal base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", hexgnl_pyramidvol)

Output:

Enter some random number = 4
Enter some random number = 6.2
Enter some random number = 15
The Volume of triangular base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 61.9752
The Volume of square base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 32.736000000000004
The Volume of pentagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 308.76
The Volume of hexagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 372.0

 

 

Python Program for Volume of Pyramid Read More »

Program for Volume and Surface Area of Frustum of Cone

Python Program for Volume and Surface Area of Frustum of Cone

Given the height, slant height, smaller and bigger radius of a frustum cone, the task is to find the volume and surface area of the frustum of a given cone in python.

Frustum of a Cone :

A frustum in geometry is the portion of a solid (typically a cone or pyramid) that lies between one or two parallel planes that cut it.
The portion of a solid between a plane parallel to the base and a right circular cone is known as the frustum of a cone.

A right circular cone is shown below:

After being cut by a plane parallel to its base, the right circular cone produces the following frustum:

which has a circular base with radius R at the bottom, a circular upper portion with radius R height h, and a slant height l.

Formulas:

volume of the frustum of a cone = 1/3 * pi * h(r2 + R2 + r*R)

The curved surface area of a frustum of a cone = pi * l(R+r)

The total surface area of a frustum of a cone = pi * l(R+r) + pi(R2 + r2)

In which,

r is the radius of the smaller circle

R is the radius of the bigger circle

l is the slant height of the frustum of a cone.

Examples:

Example1:

Input:

Given smaller radius = 3
Given bigger radius = 6
Given slant height = 8
Given height  = 10

Output:

The Volume of above given Frustum of Cone =  659.7344572538565
The curved surface area of the above given frustum of Cone =  226.1946710584651
The total surface area of the above given frustum of Cone :  367.5663404700058

Example2:

Input:

Given smaller radius = 4
Given bigger radius = 7
Given slant height = 10
Given height = 15

Output:

The Volume of above given Frustum of Cone = 1460.8405839192537
The curved surface area of the above given frustum of Cone = 345.57519189487726
The total surface area of the above given frustum of Cone : 549.7787143782139

Program for Volume and Surface Area of Frustum of Cone in Python

Below are the ways to find the volume and surface area of the frustum of a given cone in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Get the value of pi using the math.pi function and store it in a variable.
  • Give the radius of the smaller circle as static input and store it in a variable.
  • Give the radius of the bigger circle as static input and store it in another variable.
  • Give the slant height of the frustum cone as static input and store it in another variable.
  • Give the height as static input and store it in another variable.
  • Calculate the volume of the frustum of a cone using the above given mathematical formula and store it in another variable.
  • Calculate the curved surface area of a frustum of a cone using the above given mathematical formula and store it in another variable.
  • Calculate the total surface area of a frustum of a cone using the above given mathematical formula and store it in another variable.
  • Print the volume of the frustum of a cone.
  • Print the curved surface area of a frustum of a cone.
  • Print the Total surface area of a frustum of a cone.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Get the value of pi using the math.pi function and store it in a variable.
pi_val = math.pi
# Give the radius of the smaller circle as static input and store it in a variable.
gvn_smallrradiuss = 3
# Give the radius of the bigger circle as static input and store it in another variable.
gvn_biggradiuss = 6
# Give the slant height of the frustum cone as static input and store it in
# another variable.
gvn_slantheigt = 8
# Give the height as static input and store it in another variable.
gvn_heigt = 10
# Calculate the volume of the frustum of a cone using the above given mathematical
# formula and store it in another variable.
rslt_vol = 1 / 3 * pi_val * gvn_heigt * \
    (gvn_smallrradiuss * gvn_smallrradiuss + gvn_biggradiuss *
     gvn_biggradiuss + gvn_smallrradiuss * gvn_biggradiuss)
# Calculate the curved surface area of a frustum of a cone using the above given
# mathematical formula and store it in another variable.
curvd_surfce_area = pi_val * gvn_slantheigt * \
    (gvn_biggradiuss + gvn_smallrradiuss)
# Calculate the total surface area of a frustum of a cone using the above given
# mathematical formula and store it in another variable.
totl_surfce_area = pi_val * gvn_slantheigt * (gvn_biggradiuss + gvn_smallrradiuss) + pi_val * \
    (gvn_smallrradiuss * gvn_smallrradiuss + gvn_biggradiuss * gvn_biggradiuss)
# Print the volume of the frustum of a cone.
print("The Volume of above given Frustum of Cone = ", rslt_vol)
# Print the curved surface area of a frustum of a cone.
print("The curved surface area of the above given frustum of Cone = ", curvd_surfce_area)
# Print the Total surface area of a frustum of a cone.
print("The total surface area of the above given frustum of Cone : ", totl_surfce_area)
#include <cmath>
#include <iostream>

using namespace std;

int main()
{
    int argc = 2;
    double pi_val = M_PI;
    double gvn_smallrradiuss = 3;
    int gvn_biggradiuss = 6;
    int gvn_slantheigt = 8;
    int gvn_heigt = 10;
    double rslt_vol
        = 1 / 3 * pi_val * gvn_heigt
          * (gvn_smallrradiuss * gvn_smallrradiuss
             + gvn_biggradiuss * gvn_biggradiuss
             + gvn_smallrradiuss * gvn_biggradiuss);
    double curvd_surfce_area
        = pi_val * gvn_slantheigt
          * (gvn_biggradiuss + gvn_smallrradiuss);
    double totl_surfce_area
        = pi_val * gvn_slantheigt
              * (gvn_biggradiuss + gvn_smallrradiuss)
          + pi_val * gvn_smallrradiuss * gvn_smallrradiuss
          + gvn_biggradiuss * gvn_biggradiuss;
    // Print the volume of the frustum of a cone.
    cout << "The Volume of above given Frustum of Cone = "
         << rslt_vol << endl;
    // Print the curved surface area of a frustum of a cone.
    cout << "The curved surface area of the above given "
            "frustum of Cone = "
         << curvd_surfce_area << endl;
    // Print the Total surface area of a frustum of a cone.
    cout << "The total surface area of the above given "
            "frustum of Cone :"
         << totl_surfce_area;
}

Output:

The Volume of above given Frustum of Cone =  659.7344572538565
The curved surface area of the above given frustum of Cone =  226.1946710584651
The total surface area of the above given frustum of Cone :  367.5663404700058

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Get the value of pi using the math.pi function and store it in a variable.
  • Give the radius of the smaller circle as user input using the float(input()) function and store it in a variable.
  • Give the radius of the bigger circle as user input using the float(input()) function and store it in another variable.
  • Give the slant height of the frustum cone as user input using the float(input()) function and store it in another variable.
  • Give the height as user input using the float(input()) function and store it in another variable.
  • Calculate the volume of the frustum of a cone using the above given mathematical formula and store it in another variable.
  • Calculate the curved surface area of a frustum of a cone using the above given mathematical formula and store it in another variable.
  • Calculate the total surface area of a frustum of a cone using the above given mathematical formula and store it in another variable.
  • Print the volume of the frustum of a cone.
  • Print the curved surface area of a frustum of a cone.
  • Print the Total surface area of a frustum of a cone.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Get the value of pi using the math.pi function and store it in a variable.
pi_val = math.pi
# Give the radius of the smaller circle as user input using the float(input()) function
# and store it in a variable.
gvn_smallrradiuss = float(input("Enter some random number = "))
# Give the radius of the bigger circle as user input using the float(input()) function 
# and store it in another variable.
gvn_biggradiuss = float(input("Enter some random number = "))
# Give the slant height of the frustum cone as user input using the float(input()) function
# and store it in another variable.
gvn_slantheigt = float(input("Enter some random number = "))
# Give the height as user input using the float(input()) function and
# store it in another variable.
gvn_heigt = float(input("Enter some random number = "))
# Calculate the volume of the frustum of a cone using the above given mathematical
# formula and store it in another variable.
rslt_vol = 1 / 3 * pi_val * gvn_heigt * \
    (gvn_smallrradiuss * gvn_smallrradiuss + gvn_biggradiuss *
     gvn_biggradiuss + gvn_smallrradiuss * gvn_biggradiuss)
# Calculate the curved surface area of a frustum of a cone using the above given
# mathematical formula and store it in another variable.
curvd_surfce_area = pi_val * gvn_slantheigt * \
    (gvn_biggradiuss + gvn_smallrradiuss)
# Calculate the total surface area of a frustum of a cone using the above given
# mathematical formula and store it in another variable.
totl_surfce_area = pi_val * gvn_slantheigt * (gvn_biggradiuss + gvn_smallrradiuss) + pi_val * \
    (gvn_smallrradiuss * gvn_smallrradiuss + gvn_biggradiuss * gvn_biggradiuss)
# Print the volume of the frustum of a cone.
print("The Volume of above given Frustum of Cone = ", rslt_vol)
# Print the curved surface area of a frustum of a cone.
print("The curved surface area of the above given frustum of Cone = ", curvd_surfce_area)
# Print the Total surface area of a frustum of a cone.
print("The total surface area of the above given frustum of Cone : ", totl_surfce_area)

Output:

Enter some random number = 4
Enter some random number = 7
Enter some random number = 10
Enter some random number = 15
The Volume of above given Frustum of Cone = 1460.8405839192537
The curved surface area of the above given frustum of Cone = 345.57519189487726
The total surface area of the above given frustum of Cone : 549.7787143782139

 

 

Python Program for Volume and Surface Area of Frustum of Cone Read More »