Posted on by Kalkicode
Code Geometric

# Calculate area of trapezoid

The problem focuses on calculating the area of a trapezoid given the lengths of its two bases and the altitude (perpendicular distance between the bases). This is a common geometric calculation used in various fields such as architecture, engineering, and construction. The trapezoid's area can be determined using a simple formula that involves the lengths of the bases and the altitude.

## Problem Statement

Given the lengths of the two bases (base1 and base2) and the altitude of a trapezoid, we need to calculate and output the area of the trapezoid. The sides and altitude are provided as inputs, and we are required to use the trapezoid area formula to find the area of the trapezoid.

## Example

Consider a trapezoid with base1 = 24, base2 = 9.3, and altitude = 8. The trapezoid is represented as follows:

``````
----------------------
/                      \
/                        \
/                          \
-----------------------------

``````

## Idea to Solve

To solve this problem, we can follow these steps:

1. Calculate the area of the trapezoid using the formula: area = ((base1 + base2) / 2) * altitude.
2. Use the provided values for base1, base2, and altitude to compute the area.

## Pseudocode

``````function trapezoid_area(base1, base2, altitude):
area = ((base1 + base2) / 2) * altitude
return area

for each test case:
base1, base2, altitude = trapezoid parameters
result = trapezoid_area(base1, base2, altitude)
print("Trapezoid Size [ base1 :", base1, ", base2 :", base2, "altitude :", altitude, "]")
print("Trapezoid Area :", result)``````

## Algorithm Explanation

1. For each test case, calculate the area of the trapezoid using the provided formula.
2. Print the trapezoid's dimensions (base lengths and altitude) and the calculated area.

## Code Solution

``````// C Program
// Calculate area of trapezoid
#include <stdio.h>

//Calculate area of trapezoid by given bases and altitude
void trapezoid_area(double base1, double base2, double altitude)
{
//Calculate area of trapezoid
double area = ((base1 + base2) / 2) * altitude;
//Display the result of trapezoid area
printf(" Trapezoid Size [ base1 : %lf, base2 : %lf, altitude : %lf ] ", base1, base2, altitude);
printf("\n Trapezoid Area : %lf\n\n", area);
}
int main()
{
//Test Cases
trapezoid_area(24, 9.3, 8);
trapezoid_area(20, 15, 32);
trapezoid_area(5.6, 9.2, 4.2);
return 0;
}``````

#### Output

`````` Trapezoid Size [ base1 : 24.000000, base2 : 9.300000, altitude : 8.000000 ]
Trapezoid Area : 133.200000

Trapezoid Size [ base1 : 20.000000, base2 : 15.000000, altitude : 32.000000 ]
Trapezoid Area : 560.000000

Trapezoid Size [ base1 : 5.600000, base2 : 9.200000, altitude : 4.200000 ]
Trapezoid Area : 31.080000
``````
``````/*
C++ program
Find area of trapezoid
*/
#include<iostream>

using namespace std;
class TrapezoidArea
{
public:
//Calculate area of trapezoid by given bases and altitude
void trapezoid_area(double base1, double base2, double altitude)
{
//Calculate area of trapezoid
double area = ((base1 + base2) / 2) *altitude;
cout << " Trapezoid Size [ base1 : " << base1 << ", base2 : " << base2 << ", altitude : " << altitude << " ] ";
cout << "\n Trapezoid Area : " << area << "\n\n";
}
};
int main()
{
TrapezoidArea obj =  TrapezoidArea();
//Test Cases
obj.trapezoid_area(24, 9.3, 8);
obj.trapezoid_area(20, 15, 32);
obj.trapezoid_area(5.6, 9.2, 4.2);
return 0;
}``````

#### Output

`````` Trapezoid Size [ base1 : 24, base2 : 9.3, altitude : 8 ]
Trapezoid Area : 133.2

Trapezoid Size [ base1 : 20, base2 : 15, altitude : 32 ]
Trapezoid Area : 560

Trapezoid Size [ base1 : 5.6, base2 : 9.2, altitude : 4.2 ]
Trapezoid Area : 31.08
``````
``````/*
Java program
Find area of trapezoid
*/
public class TrapezoidArea
{
//Calculate area of trapezoid by given bases and altitude
public void trapezoid_area(double base1, double base2, double altitude)
{
//Calculate area of trapezoid
double area = ((base1 + base2) / 2) * altitude;
//Display the result of trapezoid area
System.out.print(" Trapezoid Size [ base1 : " + base1 + ", base2 : " + base2 + ", altitude : " + altitude + " ] ");
System.out.print("\n Trapezoid Area : " + area + "\n\n");
}
public static void main(String[] args)
{
TrapezoidArea obj = new TrapezoidArea();
//Test Cases
obj.trapezoid_area(24, 9.3, 8);
obj.trapezoid_area(20, 15, 32);
obj.trapezoid_area(5.6, 9.2, 4.2);
}
}``````

#### Output

`````` Trapezoid Size [ base1 : 24.0, base2 : 9.3, altitude : 8.0 ]
Trapezoid Area : 133.2

Trapezoid Size [ base1 : 20.0, base2 : 15.0, altitude : 32.0 ]
Trapezoid Area : 560.0

Trapezoid Size [ base1 : 5.6, base2 : 9.2, altitude : 4.2 ]
Trapezoid Area : 31.08

``````
``````/*
C# program
Find area of trapezoid
*/
using System;
public class TrapezoidArea
{
//Calculate area of trapezoid by given bases and altitude
public void trapezoid_area(double base1, double base2, double altitude)
{
//Calculate area of trapezoid
double area = ((base1 + base2) / 2) * altitude;
Console.Write(" Trapezoid Size [ base1 : " + base1 + ", base2 : " + base2 + ", altitude : " + altitude + " ] ");
Console.Write("\n Trapezoid Area : " + area + "\n\n");
}
public static void Main(String[] args)
{
TrapezoidArea obj = new TrapezoidArea();
//Test Cases
obj.trapezoid_area(24, 9.3, 8);
obj.trapezoid_area(20, 15, 32);
obj.trapezoid_area(5.6, 9.2, 4.2);
}
}``````

#### Output

`````` Trapezoid Size [ base1 : 24, base2 : 9.3, altitude : 8 ]
Trapezoid Area : 133.2

Trapezoid Size [ base1 : 20, base2 : 15, altitude : 32 ]
Trapezoid Area : 560

Trapezoid Size [ base1 : 5.6, base2 : 9.2, altitude : 4.2 ]
Trapezoid Area : 31.08
``````
``````<?php
/*
Php program
Find area of trapezoid
*/
class TrapezoidArea
{
//Calculate area of trapezoid by given bases and altitude
public 	function trapezoid_area(\$base1, \$base2, \$altitude)
{
//Calculate area of trapezoid
\$area = ((\$base1 + \$base2) / 2) *\$altitude;
//Display the result of trapezoid area
echo(" Trapezoid Size [ base1 : ". \$base1 .", base2 : ". \$base2 .", altitude : ". \$altitude ." ] ");
echo("\n Trapezoid Area : ". \$area ."\n\n");
}
}

function main()
{
\$obj = new TrapezoidArea();
//Test Cases
\$obj->trapezoid_area(24, 9.3, 8);
\$obj->trapezoid_area(20, 15, 32);
\$obj->trapezoid_area(5.6, 9.2, 4.2);
}
main();``````

#### Output

`````` Trapezoid Size [ base1 : 24, base2 : 9.3, altitude : 8 ]
Trapezoid Area : 133.2

Trapezoid Size [ base1 : 20, base2 : 15, altitude : 32 ]
Trapezoid Area : 560

Trapezoid Size [ base1 : 5.6, base2 : 9.2, altitude : 4.2 ]
Trapezoid Area : 31.08
``````
``````/*
Node Js program
Find area of trapezoid
*/
class TrapezoidArea
{
//Calculate area of trapezoid by given bases and altitude
trapezoid_area(base1, base2, altitude)
{
//Calculate area of trapezoid
var area = ((base1 + base2) / 2) *altitude;
//Display the result of trapezoid area
process.stdout.write(" Trapezoid Size [ base1 : " + base1 + ", base2 : " + base2 + ", altitude : " + altitude + " ] ");
process.stdout.write("\n Trapezoid Area : " + area + "\n\n");
}
}

function main(args)
{
var obj = new TrapezoidArea();
//Test Cases
obj.trapezoid_area(24, 9.3, 8);
obj.trapezoid_area(20, 15, 32);
obj.trapezoid_area(5.6, 9.2, 4.2);
}
main();``````

#### Output

`````` Trapezoid Size [ base1 : 24, base2 : 9.3, altitude : 8 ]
Trapezoid Area : 133.2

Trapezoid Size [ base1 : 20, base2 : 15, altitude : 32 ]
Trapezoid Area : 560

Trapezoid Size [ base1 : 5.6, base2 : 9.2, altitude : 4.2 ]
Trapezoid Area : 31.08
``````
``````#   Python 3 program
#   Find area of trapezoid

class TrapezoidArea :
# Calculate area of trapezoid by given bases and altitude
def trapezoid_area(self, base1, base2, altitude) :
# Calculate area of trapezoid
area = ((base1 + base2) / 2) * altitude

# Display the result of trapezoid area
print(" Trapezoid Size [ base1 : ", base1 ,", base2 : ", base2 ,", altitude : ", altitude ," ] ", end = "")
print("\n Trapezoid Area : ", area ,"\n\n", end = "")

def main() :
obj = TrapezoidArea()
# Test Cases
obj.trapezoid_area(24, 9.3, 8)
obj.trapezoid_area(20, 15, 32)
obj.trapezoid_area(5.6, 9.2, 4.2)

if __name__ == "__main__": main()``````

#### Output

`````` Trapezoid Size [ base1 :  24 , base2 :  9.3 , altitude :  8  ]
Trapezoid Area :  133.2

Trapezoid Size [ base1 :  20 , base2 :  15 , altitude :  32  ]
Trapezoid Area :  560.0

Trapezoid Size [ base1 :  5.6 , base2 :  9.2 , altitude :  4.2  ]
Trapezoid Area :  31.08
``````
``````#   Ruby program
#   Find area of trapezoid

class TrapezoidArea

# Calculate area of trapezoid by given bases and altitude
def trapezoid_area(base1, base2, altitude)

# Calculate area of trapezoid
area = ((base1 + base2) / 2) * altitude
# Display the result of trapezoid area
print(" Trapezoid Size [ base1  : ", base1 ,", base2  : ", base2 ,", altitude  : ", altitude ," ] ")
print("\n Trapezoid Area  :", area ,"\n\n")
end
end
def main()

obj = TrapezoidArea.new()
# Test Cases
obj.trapezoid_area(24, 9.3, 8)
obj.trapezoid_area(20, 15, 32)
obj.trapezoid_area(5.6, 9.2, 4.2)
end
main()``````

#### Output

`````` Trapezoid Size [ base1  : 24, base2  : 9.3, altitude  : 8 ]
Trapezoid Area  :133.2

Trapezoid Size [ base1  : 20, base2  : 15, altitude  : 32 ]
Trapezoid Area  :544

Trapezoid Size [ base1  : 5.6, base2  : 9.2, altitude  : 4.2 ]
Trapezoid Area  :31.08

``````
``````/*
Scala program
Find area of trapezoid
*/
class TrapezoidArea
{
//Calculate area of trapezoid by given bases and altitude
def trapezoid_area(base1: Double, base2: Double, altitude: Double): Unit = {
//Calculate area of trapezoid
var area: Double = ((base1 + base2) / 2) * altitude;
//Display the result of trapezoid area
print(" Trapezoid Size [ base1 : " + base1 + ", base2 : " + base2 + ", altitude : " + altitude + " ] ");
print("\n Trapezoid Area : " + area + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: TrapezoidArea = new TrapezoidArea();
//Test Cases
obj.trapezoid_area(24, 9.3, 8);
obj.trapezoid_area(20, 15, 32);
obj.trapezoid_area(5.6, 9.2, 4.2);
}
}``````

#### Output

`````` Trapezoid Size [ base1 : 24.0, base2 : 9.3, altitude : 8.0 ]
Trapezoid Area : 133.2

Trapezoid Size [ base1 : 20.0, base2 : 15.0, altitude : 32.0 ]
Trapezoid Area : 560.0

Trapezoid Size [ base1 : 5.6, base2 : 9.2, altitude : 4.2 ]
Trapezoid Area : 31.08
``````
``````/*
Swift program
Find area of trapezoid
*/
class TrapezoidArea
{
//Calculate area of trapezoid by given bases and altitude
func trapezoid_area(_ base1: Double, _ base2: Double, _ altitude: Double)
{
//Calculate area of trapezoid
let area: Double = ((base1 + base2) / 2) * altitude;

//Display the result of trapezoid area
print(" Trapezoid Size [ base1 : ", base1 ,", base2 : ", base2 ,", altitude : ", altitude ," ] ", terminator: "");
print("\n Trapezoid Area : ", area ,"\n\n", terminator: "");
}
}
func main()
{
let obj: TrapezoidArea = TrapezoidArea();
//Test Cases
obj.trapezoid_area(24, 9.3, 8);
obj.trapezoid_area(20, 15, 32);
obj.trapezoid_area(5.6, 9.2, 4.2);
}
main();``````

#### Output

`````` Trapezoid Size [ base1 :  24.0 , base2 :  9.3 , altitude :  8.0  ]
Trapezoid Area :  133.2

Trapezoid Size [ base1 :  20.0 , base2 :  15.0 , altitude :  32.0  ]
Trapezoid Area :  560.0

Trapezoid Size [ base1 :  5.6 , base2 :  9.2 , altitude :  4.2  ]
Trapezoid Area :  31.08
``````

## Output Explanation

The provided code runs three test cases and calculates the areas of trapezoids with different base lengths and altitudes. The output includes the trapezoid's dimensions and the corresponding calculated areas.

## Time Complexity

The time complexity of this code is O(1) for each test case. The calculations involve basic arithmetic operations, which take constant time. Therefore, the overall time complexity remains constant for each test case.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post