Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to calculate the ‘Catalan number’ as defined below: Cn= (1/n

ID: 3886599 • Letter: W

Question

Write a C++ program to calculate the ‘Catalan number’ as defined below: Cn= (1/n+1)((2n!)/(2n!(2n-n)!) where n0, C0=1, and(nk) = n!/(k!(n-k)!) DESCRIPTION: Print to screen the following choices and prompt the user for a number between 0 and 3. Print an error if the number is not between 0 and 3. Otherwise, ask for n if choice is 1 or 3, and ask for n and k if choice is 2. Calculate the required value and print to screen. Repeat the same until the selected choice is 0. Choices: 0: Exit program 1: Find the factorial of n 2: Find the combination of n things k at a time 3: Find the Catalan number of n as defined above REQUIREMENT: Use SWITCH to interpret a selected choice Create a function for each of choices 1, 2, and 3

Explanation / Answer

#include <iostream>
using namespace std;

int factorial(int n)
{
if (n == 0)
return 1;
else
return n*factorial(n-1);//recursive call
  
}
unsigned long catalan(int n)
{

if(n == 0)
return 1;
else
return (factorial(2*n)/(factorial(n+1)*factorial(n))); //calatlon number
}

int combination(int n,int k)
{
return factorial(n)/(factorial(k)*factorial(n-k));
}

int main()
{
int option,n,k;
  
do
{
cout<<" 1. Find the factorial of n";
cout<<" 2. Find the combination of n things k at a time ";
cout<<" 3. Find the Catalan number of n";
cout<<" 0. Quit";
cout<<" Enter selection: ";
cin>>option;
  
switch(option)
{
case 1: cout<<"Enter the value of n : ";
cin>>n;
cout<<" Factorial of "<<n<<" = "<<factorial(n);
break;
case 2: cout<<" Enter the value of n : ";
cin>>n;
cout<<" Enter the value of k : ";
cin>>k;
cout<<" Combination of "<<n<<" things "<< k<<" at a time = "<<combination(n,k);
break;   
case 3: cout<<" Enter Catalan number to calculate: ";
cin>>n;
cout<<" Catalan number at "<<n <<" is "<<catalan(n);
break;
case 0: break;
default: cout<<" Invalid option";
break;
}
  
}while(option != 0);
  
return 0;
}

Output:

1. Find the factorial of n
2. Find the combination of n things k at a time
3. Find the Catalan number of n
0. Quit
Enter selection: 1 Enter the value of n : 5
Factorial of 5 = 120
1. Find the factorial of n
2. Find the combination of n things k at a time
3. Find the Catalan number of n
0. Quit
Enter selection: 2
Enter the value of n : 5
Enter the value of k : 3
Combination of 5 things 3 at a time = 10
1. Find the factorial of n
2. Find the combination of n things k at a time
3. Find the Catalan number of n
0. Quit
Enter selection: 3
Enter Catalan number to calculate: 6
Catalan number at 6 is 132
1. Find the factorial of n
2. Find the combination of n things k at a time
3. Find the Catalan number of n
0. Quit
Enter selection: 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote