-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnCrnPr.cpp
53 lines (38 loc) · 878 Bytes
/
nCrnPr.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
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
C++ program to find nCr and nPr: This code calculate nCr which is n!/(r!*(n-r)!) and nPr = n!/(n-r)!*/
#include<iostream.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
int main()
{
int n, r;
long ncr, npr;
cout<<"Enter the value of n and r\n";
cin>>n>>r;
ncr = find_ncr(n, r);
npr = find_npr(n, r);
cout<<n<<" C "<<r <<endl <<ncr;
cout<<n<<" P "<<r <<endl<<npr;
return 0;
}
long find_ncr(int n, int r)
{
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
long find_npr(int n, int r)
{
long result;
result = factorial(n)/factorial(n-r);
return result;
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}