Search



Tuesday, 17 February 2015

nCr nPr

In this program, we have to find combinations & permutations ( nCr & nPr ) . So, formulas are :- 

1. nCr = n! / [ (n-r)! * r! ]                      2.  nPr = n! / [ (n-r)!  ]

Here , facto function is used to calculate factorial of any no. passed to it ( like n!, r! , (n-r)! ) & their answers are stored in ncr & npr respectively & displayed..

 

Program to find nCr & nPr.

#include<stdio.h>
#include<conio.h>
long facto(int n);
void main()
{ long ncr,npr;
int n,r;
clrscr();
printf("\n\n program to find nCr & nPr");
printf("\n enter value of n : ");
scanf("%d",&n);
printf("\n enter value of r : ");
scanf("%d",&r);
ncr = facto(n)/(facto(r)*facto(n-r));
npr = facto(n)/facto(n-r);
printf("\n nPr = %ld",npr);
printf("\n nCr = %ld",ncr);
getch();
}
long facto(int n)
{ int c;
long ans = 1;
for(c=1;c<=n;c++)
{ ans = ans*c;
}
return ans;
}


No comments:

Post a Comment

Guys if you think something is wrong or should be edit than please do comment.