-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpnc.cpp
More file actions
63 lines (56 loc) · 1.37 KB
/
pnc.cpp
File metadata and controls
63 lines (56 loc) · 1.37 KB
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
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int factorial(int number) {
if (number == 0) {
return 1;
}
else{
return number*factorial(number-1);
}
}
int permutations(int n,int r) {
return factorial(n)/factorial(n-r);
}
int combinations(int n,int r) {
return factorial(n)/(factorial(r)*factorial(n-r));
}
int main(int argc, char const *argv[]) {
system("cls");
int choice;
int n;
int r;
int answer;
do {
system("cls");
cout << "\t***** PERMUTATIONS AND COMBINATIONS *****\n\n";
cout << "1. PERMUTATIONS\n2. COMBINATIONS\n";
cout << "3. EXIT\n\n";
cout << "Enter the choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "Enter n: ";
cin >> n;
cout << "Enter r: ";
cin >> r;
answer = permutations(n,r);
cout << n << "P" << r << " is " << answer;
getch();
break;
case 2: cout << "Enter n: ";
cin >> n;
cout << "Enter r: ";
cin >> r;
answer = combinations(n,r);
cout << n << "C" << r << " is " << answer;
getch();
break;
case 3: exit(0);
default: cerr << "ERROR: Not a valid input. Try again.";
break;
}
} while(choice != 3);
getch();
return 0;
}