-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutations.cpp
57 lines (50 loc) · 1.14 KB
/
permutations.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
#include <iostream>
using namespace std;
int factorial (int number) {
if(number == 0 || number == 1)
return 1;
else return number*factorial(number-1);
}
int permutation (int n, int r){
return factorial(n)/factorial(n-r);
}
int combination (int n, int r){
return permutation(n,r)/factorial(r);
}
int main () {
int n, r;
int choice;
x: cout << "Enter the integers ('n' and 'r' in that order): ";
if(!(cin >> n >> r)){
cout << "Please enter only integers.\n";
cin.clear();
cin.ignore(100000, '\n');
goto x;
}
else {
y: cout << "Please choose an option:\n"<< "1. Permutation - Press 1\n2. Combination - Press 2\nEnter choice here: " << endl;
if(!(cin >> choice)){
cout << "Please enter only integers.\n" << endl;
cin.clear();
cin.ignore(100000, '\n');
goto y;
}
if(choice !=1 && choice !=2){
cout << "Not a valid choice. Try again.\n";
cin.clear();
cin.ignore(100000, '\n');
goto y;
}
else{
switch(choice){
case 1:
cout << "The permutation " << n << "P" << r << " is: " << permutation(n,r) << endl;
break;
case 2:
cout << "The combination " << n << "C" << r << " is: " << combination(n,r) << endl;
break;
}
}
}
return(0);
}