C Program to find nCr and nPr( Use recursive function to find the factorial)

 #include<stdio.h>

main()

{

long fact(int);

int n,r,i;

long npr,ncr;

clrscr();

printf("\nEnter the value for N and R\n");

scanf("%d%d",&n,&r);

if(r<=n)

{

npr=fact(n)/fact(n-r);

ncr=npr/fact(r);

printf("\n%dP%d= %ld",n,r,npr);

printf("\n%dC%d= %ld",n,r,ncr);

}

else

{

printf("\nWrong Input (R<N)\n");

}

getch();

}

long fact(int n)

{

if(n!=0)

return(n*fact(n-1));

else

return (1);

}



Output:

  1. Enter the value for N and R

11

7

11P7= 1663200

11C7= 330

  1.  Enter the value for N and R

6

2

6P2= 30

6C2= 15

Post a Comment

Previous Post Next Post