Swift Program To Find The Area of a Trapezium

This tutorial will discuss how to write a swift program to find the area of a trapezium.

A trapezium is a quadrilateral with at least a pair of opposite parallel sides. The parallel sides are known as the base and the non-parallel sides are known as the legs of the trapezium. The distance between the parallel sides is known as the height of the trapezium.

In a trapezium, an area is known as the space that is enclosed inside the boundaries of the trapezium in the two-dimensional plane. Suppose we have a board, now the area of the trapezium helps us to find how much paint we required to cover the top of the board. We can calculate the area of the trapezium with the help of the parallel sides and the distance between them.

Algorithm to find Area of a Trapezium

• Step 1 − Define two variables

• Step 2 − Enter the value of those variables

• Step 3 − Perform Area of Trapezium formula for those two variables

• Step 4 − Print the output

Formula

Following is the formula of the area of a trapezium −

Area = ((X + Y)/2)H

Where,

X and Y represent the length of the parallel sides or bases of the trapezium as shown in the above diagram

H represents the height between the parallel sides

Example 1

The following program shows how to calculate the area of a trapezium.

import Foundation
import Glibc

var Side1 = 12
var Side2 = 20
var Height = 10

var AreaOfTrapezium = (Side1 + Side2)*(Height / 2)

print("Side 1 of Trapezium:", Side1)
print("Side 2 of Trapezium:", Side2)
print("Height between sides:", Height)
print("Final area of the Trapezium: ", AreaOfTrapezium)

Output

Side 1 of Trapezium: 12
Side 2 of Trapezium: 20
Height between sides: 10
Final area of the Trapezium: 160

In the above code, we find the area of the trapezium using the mathematical formula as shown in the below code −

var AreaOfTrapezium = (Side1 + Side2)*(Height / 2)


Here, the two sides of the trapezium are 12 and 20 and the height of the trapezium is 19 so the area of the trapezium is 160.

Example 2

The following program shows how to calculate the area of the trapezium with user-defined input.

import Foundation
import Glibc

print("Please enter height between the two sides")

var AreaOfTrapezium = (TrapHeight/2) * (TrapSide1 + TrapSide2)

print("Entered Side 1:", TrapSide1)
print("Entered Side 2:", TrapSide2)
print("Entered Height:", TrapHeight)
print("Final area of the Trapezium: ", AreaOfTrapezium)

STDIN Input

Please enter side 1- 18
Please enter height between the two sides 8

Output

Entered Side 1: 18
Entered Side 2: 14
Entered Height: 8
Final area of the Trapezium: 128

In the above code, we find the area of a trapezium using the mathematical formula as shown in the below code −

var AreaOfTrapezium = (TrapHeight/2) * (TrapSide1 + TrapSide2)


Here, the height and two sides of the trapezium are given by the user at run time and using these values the area of the trapezium is calculated which is 128.