C++ program to find Permutation and Combination

// Program to find permutation(nPr) and combination(nCr)
#include <iostream>
using namespace std;
//recursive function to find factorial
 int fact(int a)
 {
    if(a>1)
    {
       return a*fact(a-1);
    }
    else
    {
       return 1;
    }
 }

int main()
 {
int num1,num2,num3,n,r,choice;
cout<<"Enter the value of n ";
cin>>n;
cout<<"Enter the value of r ";
cin>>r;
if(n<r)
   {
      cout<<"The value of n must be greater than r "<<endl;
      cout<<"Enter again ";
      cin>>n>>r;
   }
cout<<"\tEnter 1 to find permutation "<<endl;
cout<<"\tEnter 2 to find Combination\n "<<endl;
cout<<"Enter Your choice ";
cin>>choice;
switch(choice)
{
   case 1:
      {
         num1=fact(n);
         num2=fact(n-r);
         num3=num1/num2;
         cout<<"nPr = "<<num3<<endl;
         break;
      }
   case 2:
      {
         num1=fact(n);
         num2=fact(n-r)*fact(r);
         num3=num1/num2;
         cout<<" nCr = "<<num3<<endl;
         break;
      }
   default:
      {
         cout<<"You Entered an incorrect choice :"<<endl;
         break;
      }
}


return 0;
}

1 comment: