Write a C++ program that will output the number of distinct ways in which you ca
ID: 3544336 • Letter: W
Question
Write a C++ program that will output the number of distinct ways in which you can pick "k" objects out of a set of "n" objects (both "n" and "k" should be positive integers). This number is given by the following formula:
C (n, k) = n! / (k! * (n-k)!)
Your program should use 2 value-returning functions. The first one should be called FACTORIAL and should return n!. The second functions should be called COMBINATIONS and should return n! / (k! * (n-k)!). Test your program for different values of n and k 5 tiomes (count-controlled loop). Check for errors in input.
Explanation / Answer
#include<iostream>
using namespace std;
int FACTORIAL(int k)
{
if(k==0) return 1;
return k*FACTORIAL(k-1);
}
int COMBINATIONS(int n,int k)
{
return FACTORIAL(n)/(FACTORIAL(k)*FACTORIAL(n-k));
}
int main()
{
int n=10;
cout <<"Enter value of n :";
cin>>n;
cout << endl;
int count =1;
while(count <=5)
{
cout << "pick "<< count <<" objects out of a set of "<< n << " objects is " << COMBINATIONS(n,count) << endl;
count++;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.