Swift Program to Find the Largest Among Three Numbers


This tutorial will discuss how to write a Swift program to find the largest among three numbers.

Given three numbers now we have to find the largest among the given three numbers. So we will compare all three numbers with each other to find the largest number.

Below is a demonstration of the same −

Suppose we enter the following input −

39, 87, 12

Following is the desired output −

Largest number is 87

Finding largest number using max() function

Swift provides an in-built method named as max() function. This function returns the maximum number among the given numbers.

Syntax

Following is the syntax of the Swift max() function −

max(num1, num2, num3)

Algorithm

The algorithm is explained below −

  • Step 1 − Declare three variables

  • Step 2 − read their values from the user.

  • Step 3 − Use the max() function to find the largest number among the three numbers.

  • Step 4 − Print the output.

Example

The following program shows how to find the largest among three numbers using the max() function.

import Foundation import Glibc print("Enter first number") var num1 = Int(readLine()!)! print(num1) print("Enter second number") var num2 = Int(readLine()!)! print(num2) print("Enter third number") var num3 = Int(readLine()!)! print(num3) var largest = max(num1, num2, num3) print("Largest number is", largest)

Output

Enter first number
23
Enter second number
1
Enter third number
10
Largest number is 23

Here in the above code, we takes three integer numbers from the user using the readLine() function that are num1 = 23, num2 = 1 and num3 = 10. Now using the max() function we compare all the three numbers with each other and display the largest number which is 23.

Finding largest number by comparing values

We can also find the largest number among three numbers by comparing all the numbers with each other using > operator.

Algorithm

The algorithm is explained below −

  • Step 1 − Create a function.

  • Step 2 − Use if-else-if statement and compare the three numbers- n1, n2, and n3 using > operator.

  • Step 3 − Call the function with three arguments

  • Step 4 − Display the output

Example

The following program shows how to find the largest among three numbers by comparing values.

import Foundation import Glibc func maximumValue(n1: Int, n2: Int, n3: Int) -> Int{ // Comparing n1, n2 and n3 with // each other to find the largest number if n1 > n2, n1 > n3{ return n1 } else if n2 > n3, n2 > n1{ return n2 } else if n3 > n2, n3 > n1{ return n3 } else if n1 == n2, n2 > n3{ return n1 } else if n2 == n3, n3 > n1{ return n2 } else{ return n1 } } print("Maximum number is-", maximumValue(n1: 2, n2: 4, n3: 6)) print("Maximum number is-", maximumValue(n1: -19, n2: 34, n3: 19))

Output

Maximum number is- 6
Maximum number is- 34

Here in the above code, we create a function named as maximumValue(), which will return the maximum number among the given three numbers. In this function, we use an if-else-if loop to compare all the numbers with each other with the help of the > operator. Now we call the function two time with different parameter values that is maximumValue(n1: 2, n2: 4, n3: 6) and maximumValue(n1: -19, n2: 34, n3: 19) and display the maximum values that are 6 out of (2, 4, 6) and 34 out of(-19, 34, 19) on the output screen.

Updated on: 05-Aug-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements