Swift Program To Perform nCr (rcombinations)


This tutorial will discuss how to write a Swift program to perform nCr(r-combination).

nCr refers to r- combination. It is used to calculate the possible arrangements where the order of the selection does not matter. Or we can say that nCr is used to select r items from the set of n items where the order of the items does not consider.

Formula

Following is the formula of nCr

nCr = (n!)/(r! * (n - r)!)

Below is a demonstration of the same −

Suppose we enter the following input.

N = 5
R = 3

Following is the desired output.

nCr = 10

Algorithm

The algorithm is explained below −

  • Step 1 − Create a function to find the factorial of the number.

  • Step 2 − Create a function to calculate the nCr named as combination().

  • Step 3 − Call the combination function with two arguments that are N and R. Here the value of N and R can be pre-defined or user-defined.

  • Step 4 − Display the final output.

Example 1

The following program shows how to perform nCr(r-combinations)

import Foundation import Glibc func factorial(N: Int) -> Int{ var output = 1 if (N > 1) { for j in 1...N{ output *= j } } return output } func combination(N: Int, R: Int) -> Int{ return factorial(N: N)/(factorial(N:R) * factorial(N:(N - R))) } print("Result of nCr is", combination(N: 5, R: 3))

Output

Result of nCr is 10

Here in the above code first we create a function named factorial(). This function takes 1 argument and returns the factorial of the given number. Now we create another function named combination(), it takes two parameters and returns the combination with the help of the mathematical formula −

func combination(N: Int, R: Int) -> Int{
    return factorial(N: N)/(factorial(N:R) * factorial(N:(N - R)))
}

Here we find the factorial of the number using the factorial() function. Now we call the combination() function with two parameters N = 5 and R = 3 and display the final value of nCr which is 10.

Example 2

The following program shows how to perform nCr(r-combinations) with user input

import Foundation import Glibc func factorial(N: Int) -> Int{ var output = 1 if (N > 1) { for j in 1...N{ output *= j } } return output } func combination(N: Int, R: Int) -> Int{ return factorial(N: N)/(factorial(N:R) * factorial(N:(N - R))) } print("Enter the value of N- ") var num1 = Int(readLine()!)! print(num1) print("Enter the value of r-")
var num2 = Int(readLine()!)! print(num2) print("Result of nCr is", combination(N: num1, R: num2))

STDIN Input

Enter the value of N- 
9
Enter the value of r-
4

Output

Result of nCr is 126

Here in the above code first we create a function named factorial(). This function takes 1 argument and returns the factorial of the given number. Now we create another function named combination(), it takes two parameters and returns the combination with the help of the mathematical formula −

func combination(N: Int, R: Int) -> Int{
   return factorial(N: N)/(factorial(N:R) * factorial(N:(N - R)))
}

Here we find the factorial of the number using the factorial() function. Now we take the value of N and R from the user that is 9 and 4 using the readLine() function. Now we call the combination() function and pass these two variables(that is N and R) as arguments and display the final result which is 126.

Updated on: 05-Aug-2022

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements