# Swift Program to Find Harmonic Mean of the Numbers

This tutorial will discuss how to write swift program to find the harmonic mean of the numbers.

Harmonic mean also known as HM is defined as the reciprocal of the average of reciprocal. Or we can say that harmonic mean is the reciprocal of the arithmetic mean by their reciprocal. Suppose a1, a2, a3, a4,.....an are the n-elements of a collection then the harmonic means is −

HM = n/[(1/a1) + 1/(a2) + 1/(a3) + 1/(a4)+...+ 1/(an)]

Below is a demonstration of the same −

Input

Suppose our given input is −

MyVal = [3.4, 1.2, 7.8, 4.5, 2.4]


Output

The desired output would be −

Harmonic mean =


### Formula

Following is the formula of harmonic mean −

HM = n/[(1/a1) + 1/(a2) + 1/(a3) + 1/(a4)+ …+ 1/(an)]


### Algorithm

Following is the algorithm −

• Step 1 − Create an array with values.

• Step 2 − Declare a variable to store the sum of the reciprocal of the elements.

• Step 3 − Run a for loop from 0 to less than the size of the array. Or we can say iterate through each element and find their reciprocal sum.

for x in 0..<arrNums.count{
sum = sum + Double(1 / arrNums[x])
}

• Step 4 − Find the harmonic mean by dividing the size of the array with the sum of the reciprocal of the elements.

var Harmonicmean = size/sum

• Step 5 − Print the output

### Example 1

The following program shows how to find the harmonic mean of the numbers.

import Foundation
import Glibc

// Creating an array
var arrNums = [2.0, 2.5, 3.2, 9.9, 6.2]

print("Original Array:", arrNums)

var sum : Double = 0.0

// Finding the reciprocal sum of all the
// elements of the array
for x in 0..<arrNums.count{
sum = sum + Double(1 / arrNums[x])
}

var size = Double(arrNums.count)

// Finding the harmonic mean
var Harmonicmean = size/sum

print("Harmonic Mean:", Harmonicmean)


### Output

Original Array: [2.0, 2.5, 3.2, 9.9, 6.2]
Harmonic Mean: 3.390289235086014


Here, in the above code, we have a double type array. Now we iterate through each element of the array to find sum of the reciprocal of the elements and store the result to sum variable after that we divide total number of elements present in the array with the sum to find the harmonic mean −

for x in 0..<arrNums.count{
sum = sum + Double(1 / arrNums[x])
}
var Harmonicmean = size/sum


So the working of the above code is −

sum = 0.0

Size = 5.0

1st iteration: sum = 0.0 + (1/ arrNums[0]) = 0 + (1/2.0) = 0.5

2nd iteration: sum = 0.5 + (1/ arrNums[1]) = 0 .5+ (1/2.5) = 0.9

3rd iteration: sum = 0.9 + (1/ arrNums[2]) = 0 .9+ (1/3.2) = 1.2125

4th iteration: sum = 1.2125 + (1/ arrNums[3]) = 1.2125 + (1/9.9) = 0.5

5th iteration: sum = 1.313510101010101 + (1/ arrNums[4]) = 1.313510101010101 + (1/6.2) = 1.4748004235907461

Sum = 1.4748004235907461

Harmonic Mean = Size/Sum= 5/1.4748004235907461 = 3.390289235086014

Hence, the harmonic mean of the array(that is [2.0, 2.5, 3.2, 9.9, 6.2]) is 3.390289235086014.

### Example 2

The following program shows how to find the harmonic mean of the numbers.

import Foundation
import Glibc

// Function to find the harmonic mean of the given array
func arrayHarmoMean(arr: [Double]) -> Double{
let arrNums = arr
var sum = 0.0

// Finding the reciprocal sum of all the
// elements of the array
for x in 0..<arrNums.count{
sum = sum + Double(1 / arrNums[x])
}
let size = Double(arrNums.count)
// Finding the harmonic mean
return size/sum
}

// Creating an array of double type
var arrVals = [2.6, 7.89, 2.89, 0.3, 2.34]

print("Original Array:", arrVals)
print("Harmonic Mean:", arrayHarmoMean(arr: arrVals))


### Output

Original Array: [2.6, 7.89, 2.89, 0.3, 2.34]
Harmonic Mean: 1.0827051109274841


Here, in the above code, we have an array named arrVals of double type. Now to find the harmonic mean of arrVals we create a function named arrayHarmoMean(). This function will return the harmonic mean. So the resultant harmonic mean is 1.0827051109274841.

Updated on: 20-Oct-2022

154 Views