Pages

Write a program in C language to find nCr and nPr

Sunday 26 January 2014

#include <iostream>
#include <conio.h>
using namespace std;

int fict(int n)
{
    int i, sum=1;
    for(i=1; i<=n; i++)
    {
         sum = sum*i;
    }
    return sum;
}

main()
{
      char status, find;
      int n, r, ans;
   
      while(status != 'n')
      {
             cout << "\n1: nCr\n2: nPr\n";
             find = getch();
             cout << "Enter value of n: ";
             cin >> n;
             cout << "Enter value of r: ";
             cin >> r;
           
             if(find == '1')
             {
                     ans = fict(n) /( fict(r) * fict(n-r));
                     cout << "\nAnswer of "<<n<<"C"<<r<<" is: "<<ans<<endl;
             }
             else if(find == '2')
             {
                     ans = fict(n) / fict(n-r);
                     cout << "\nAnswer of "<<n<<"P"<<r<<" is: "<<ans<<endl;
             }
             else
             {
                     cout << "Enter valid input."<<endl;
             }
           
           
             cout << "You like to calculate again? n/y.";
             status = getch();
      }
       
      system("pause");  
}

 

Search Box

Most Reading

Contact Form

Name

Email *

Message *