-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorials,premutation and combination.cpp
96 lines (96 loc) · 2.71 KB
/
factorials,premutation and combination.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
using namespace std;
int main()
{
long int i, fact = 1, n, n1, n2, n3;
do
{
cout << "Press 1 to print factorial of number" << endl;
cout << "Press 2 to print nPr" << endl;
cout << "Press 3 to print nCr (Method 1)" << endl;
cout << "Press 4 to print nCr (Method 2)" << endl;
cout << "Enter 5 to Exit " << endl;
cin >> i;
if (i == 1)
{
cout << "Enter any number : ";
cin >> n;
for (int j = 1; j <= n; j++)
{
fact *= j;
}
cout << "The Factorial is : " << fact << endl;
}
if (i == 2)
{
cout << "Enter n : ";
cin >> n1;
cout << "Enter r : ";
cin >> n2;
if (n1 > n2)
{
n3 = n1 - n2;
for (int k = n1; k > n3; k--)
{
fact = fact * k;
}
cout << "Th nPr is : " << fact << endl;
}
else
cout << "r is greater then n so condition invalid";
}
if (i == 3)
{
long int fact1 = 1, fact2 = 1, c;
cout << "Enter n : ";
cin >> n1;
cout << "Enter r : ";
cin >> n2;
if (n1 > n2)
{
n3 = n1 - n2;
for (int i = 1; i <= n1; i++)
{
fact *= i;
}
for (int j = 1; j <= n3; j++)
{
fact1 *= j;
}
for (int k = 1; k <= n2; k++)
{
fact2 *= k;
}
c = fact / (fact1 * fact2);
cout << "The nCr is : " << c << endl;
}
else
cout << "r is greater then n so condition invalid";
}
if (i == 4)
{
long int fact1 = 1, c;
cout << "Enter n : ";
cin >> n1;
cout << "Enter r : ";
cin >> n2;
if (n1 > n2)
{
n3 = n1 - n2;
for (int k = n1; k > n3; k--)
{
fact = fact * k;
}
for (int l = 1; l <= n2; l++)
{
fact1 *= l;
}
c = fact / fact1;
cout << "The nCr is : " << c << endl;
}
else
cout << "r is greater then n so condition invalid";
}
} while (i == 5);
return 0;
}