- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

- Related Articles
- Java Program to Find the Largest Among Three Numbers
- Haskell program to find the largest among three numbers
- Kotlin Program to Find the Largest Among Three Numbers
- C program to Find the Largest Number Among Three Numbers
- C++ Program to Find Largest Number Among Three Numbers
- How to Find the Largest Among Three Numbers in the Golang program?
- Java program to Largest number among three numbers
- How to Find the Largest Among Three Numbers using Python?
- Program to find largest of three numbers - JavaScript
- Java program to find largest of the three numbers using ternary operators
- C++ program to find the smallest element among three elements
- Swift Program to Find Harmonic Mean of the Numbers
- Swift Program to Find Geometric Mean of the Numbers
- Swift Program to find the EVEN numbers from the array
- Swift Program to find the prime numbers from the array