Tuesday, April 6, 2021

18.19. Write a function that gets two positive integers and returns nCr (Combination).

#include<stdio.h>
#include<conio.h>
long nCr(int n, int r)
{
long p;
int i;
if(n-r<r)
r=n-r;
p=1;
for(i=1;i<=r;p=p*(n-i+1)/i,i++);
return p;
}
void main()
{
int n, r;
clrscr ();
printf ("Enter first number: ");
scanf("%d",&n):
printf ("Enter second number: ");
scanf ("&d", &r):
printf ("
nCr= %ld", nCr(n,r)); 
getch ();
}

No comments:

Post a Comment