- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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 Factorial of a number

This tutorial will discuss how to write a Swift program to find factorial of a number.

A Factorial of a non-negative number is calculated by multiplying the number with every whole number till 1 for example, 6! = 6 x 5 x 4 x 3 x 2 x 1 = 720 which means the factorial of 6 is 720.

The general form of factorial is −

M! = m x (m - 1) x (m - 2) x (m - 3) …. X 1

## Formula

Following is the formula of factorial −

M! = M * (M - 1)!

**Below is a demonstration of the same** −

Suppose our given input is-

Enter the number - 10

Following is the desired output because 10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1

Final result - 3628800

We can calculate the factorial of the number using any of the following ways-

## Factorial using recursion

We can calculate the factorial using recursion. Recursion is a process in which a function is call itself to solve a problem.

### Algorithm

**Step 1**− Create a function**Step 2**− Use if statement to check if the given number is 0 then return 1. Otherwise, return the factorial by calling itself.**Step 3**− Declare a variable whose value can be pre-defined or user-defined.**Step 4**− Call the function and pass the defined variable into it as an argument**Step 5**− Print the output.

### Example

The following program shows how to calculate the factorial of a number using recursion −

import Foundation import Glibc func factorialValue(fNumber: Int) -> Int{ if fNumber == 0{ return 1 } else { return fNumber * factorialValue(fNumber:fNumber-1) } } var value = 4 var output = factorialValue(fNumber: value) print("Number is \(value)") print("So the factorial is \(output)")

### Output

Number is 4 So the factorial is 24

Here in the above code, we create a recursive function named as factorialValue(). This function is initially called with value = 4 as an argument. In this function first, we check if the given number is 0 or not, it is the base condition which stop the recursive function. If the given number is not 0 then will find the factorial of the number by multiplying the number with the function itself till fNumber become 0.

return fNumber * factorialValue(fNumber:fNumber-1)

Here the function call itself by decreasing the fNumber by 1 and this process will continue until the value of fNumber become 0. So the working of the factorialValue() function is −

1st function call with 4: factorialValue(4) = 4 * factorialValue(3) 2nd function call with 3: factorialValue(3) = 3 * factorialValue(2) 3rd function call with 2: factorialValue(2) = 2 * factorialValue(1) 4th function call with 1: factorialValue(1) = 1 * factorialValue(0) 5th function call with 0: factorialValue(0) = 1(According to our condition the function return 1 when fNumber is 0) Returned from 5th function call: 1 Returned from 4th function call: 1 * 1 = 1 Returned from 3rd function call: 2 * 1 = 2 Returned from 2nd function call: 3 * 2 = 6 Returned from 1st function call: 4 * 6 = 24 Display the factorial of 4 which is 24.

## Factorial using for loop

We can calculate the factorial of a number using for loop. Iterative methods are much cheaper than recursion.

### Algorithm

The algorithm is explained below −

**Step 1**− Create a function**Step 2**− Declare a variable with value- output = 1.**Step 3**− Use if statement to check if the given number is greater than 1.**Step 4**− Use for loop iterate from range 1 to the given number and find the factorial.**Step 5**− Declare a variable whose value can be pre-defined or user-defined.**Step 6**− Call the function and pass the defined variable into it as an argument.**Step 7**− Print the output.

### Example

The following program shows how to calculate the factorial of a number using for loop −

import Foundation import Glibc func factorialValue(fNumber: Int) -> Int{ var output = 1 if (fNumber > 1) { for j in 1...fNumber{ output *= j } } return output } var value = 3 var output = factorialValue(fNumber: value) print("Number is \(value)") print("So the factorial is \(output)")

### Output

Number is 3 So the factorial is 6

Here in the above code, we create a function named as factorialValue(). In this function, we create a variable named as output with value 1 this variable stores the final output. Now we use if statement to check the given number is greater than 1, when the condition is true the control enter in the for loop with range starting from 1 to fNumber to find the factorial of the given number by multiplying the number with its lower number and return the final output. Now we create a variable named “value” with value 3. Now we call the factorialValue() function and pass the “value” variable to the function as an argument and assign the result to the “output” variable and display the factorial of 3 which is 6.

## Factorial using while loop

We can calculate the factorial of a number using a while loop because iterative methods are cheaper as compared to a recursive method.

### Algorithm

The algorithm is explained below −

**Step 1**− Create a function**Step 2**− Declare two variables with values- output *= 1 and k = 1.**Step 3**− Run while statement with condition k <= fNumber and find the factorial.**Step 4**− Declare a variable whose value can be pre-defined or user-defined.**Step 5**− Call the function and pass the defined variable into it as an argument.**Step 6**− Print the output.

### Example

The following program shows how to calculate the factorial of a number using a while loop −

import Foundation import Glibc func factorialValue(fNumber: Int) -> Int{ var output = 1 var k = 1 while(k <= fNumber){ output *= k k += 1 } return output } var value = 8 var output = factorialValue(fNumber: value) print("Number is \(value)") print("So the factorial is \(output)")

### Output

Number is 8 So the factorial is 40320

Here in the above code, we create a function named as factorialValue. In this function, we create two variables with values named as output *= 1 and k = 1. Now run the while loop with condition k <= fNumber. This loop runs till the given condition is true and finds the factorial of the number. Now we create a variable named “value” with value 8. Now we call the factorialValue() function and pass the “value” variable to the function as an argument and assign the result to the “output" variable and display the factorial of 8 which is 40320.

- Related Articles
- Java Program to Find Factorial of a number
- Kotlin Program to Find Factorial of a number
- 8085 program to find the factorial of a number
- 8086 program to find the factorial of a number
- Python program to find factorial of a large number
- C++ Program to Find Factorial of a Number using Iteration
- C++ Program to Find Factorial of a Number using Recursion
- Java Program to Find Factorial of a Number Using Recursion
- Haskell Program To Find The Factorial Of A Positive Number
- C++ Program to Find Factorial of a Number using Dynamic Programming
- C++ program to find first digit in factorial of a number
- Python Program to find the factorial of a number without recursion
- Java program to find the factorial of a given number using recursion
- Python Program for factorial of a number
- Write a Golang program to find the factorial of a given number (Using Recursion)