-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutation-combination.cpp
62 lines (56 loc) · 1.28 KB
/
permutation-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
#include <iostream>
#include <stdexcept>
unsigned int factorial(unsigned int number)
{
unsigned int answer = 1;
for(unsigned int i = 1; i <= number;++i)
{
answer*=i;
}
return answer;
}
int permutation(unsigned int a, unsigned int b)
{
if(a <= 0 || b <=0)
{
throw std::runtime_error("Input must be greater than 0");
}
return factorial(a)/factorial(a-b);
}
int combination(unsigned int a, unsigned int b)
{
if(a <= 0 || b <=0)
{
throw std::runtime_error("Input must be greater than 0");
}
int p = permutation(a,b);
return p/factorial(b);
}
int main()
{
std::cout << "This program calculates Permuatation or Combination \n";
std::cout << "Enter P for Permutation or C or Combination \n";
char c;
std::cin >> c;
std::cout << "Enter two numbers, first number is different possible options \n";
std::cout << "Enter second number,number how many options you can pick at a time \n";
int f,s;
std::cin >> f >> s;
if(!std::cin)
{
std::cout << "You must enter two numbers \n";
return -1;
}
switch(c)
{
case 'P':
std::cout << permutation(f,s) << '\n';
break;
case 'C':
std::cout << combination(f,s) << '\n';
break;
default:
std::cout << "Unrecognized input: " << c << '\n';
}
return 1;
}