Friday 26 September 2014

Choose k objects from n distinct objects

Write a C program that calculates the number of ways to choose k objects from n distinct objects. 'k' and 'n' both are integers.

Input Format:
First line contains the value of n, where 0<=n<=10
Second line contains the value of k, where k>=0

Output Format:
One line containing the number of ways to chose the objects

Note: In this question you are not given main() so you have to write the complete program.



#include<stdio.h>
long fact(long a);
int main()
{int n,k,temp;
scanf("%d",&n);
scanf("%d",&k);
long int d,den1,den2,ans1;
d=fact(n);
den1=fact(k);
temp=n-k;den2=fact(temp);
ans1=den1*den2;
ans1=d/ans1;
printf("%ld",ans1);
return 0;

}

long fact(long a)
{int i;long int ans=1;
for(i=1;i<=a;i++)
ans=ans*i;
return ans;
}

1 comment: