Saturday, 4 December 2010

write a function for ncr and npr

//#include "fact.c"
#include<stdio.h>
#include<conio.h>
long double fact(int n)
{
 long double prod=1;
 int i;
 for(i=1;i<=n;i++)
 {
  prod=prod*i;
 }
 return(prod);
}
long double fact(int);//function prototype
int ncr(int n,int r)
{
 return(fact(n)/(fact(r)*fact(n-r)));
}
long npr(int n, int r)
{
 return(fact(n)/fact(n-r));
}
void main()
{
 int n,r,ncr(int,int);
 long npr(int,int);
 printf("enter values of n and r\n");
 scanf("%d%d",&n,&r);//reading values
 if(n>=r)
 {
  printf("ncr is %d\n",ncr(n,r));
  printf("npr is %d\n",npr(n,r));
 }
 else
 {
  printf("value of n can not be <r\n");
 }
 getch();
}
/*
enter value n and r
5
2
ncr=10;
npr=20
*/
c programs

No comments:

Post a Comment