-
Notifications
You must be signed in to change notification settings - Fork 0
/
tute04.cpp
40 lines (33 loc) · 753 Bytes
/
tute04.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*Exercise 4 - Functions
Write a program to calculate the function called nCr which is defined as
nCr = n!/ r!(n−r)!
Where n! is the factorial of n.
Implement the functions
long Factorial(int no);
long nCr(int n, int r);
Do not modify the main function.*/
#include <iostream>
long Factorial(int no);
long nCr(int n, int r);
int main() {
int n, r;
std::cout << "Enter a value for n ";
std::cin >> n;
std::cout << "Enter a value for r ";
std::cin >> r;
std::cout << "nCr = ";
std::cout << nCr(n,r);
std::cout << std::endl;
return 0;
}
long Factorial(int no)
{
int res = 1;
for (int i = 2; i <= no; i++)
res = res * i;
return res;
}
long nCr(int n , int r)
{
return Factorial(n) / (Factorial(r) * Factorial(n - r));
}