C program for combination. Combination is way of
selecting a set of things out of a group. C program made to find
permutation is similar. The main difference between permutation and
combination is that, in
combination order does not matter while
in permutation order matters. With combination we can find in how many
ways can n objects or a group of an objects can be selected if r objects
are selected at a time. To find Combination we need to know how
factorial of of number is calculated in c program. You can see following
two programs to implement c program for factorial :
- C program to find factorial of a number
- C program to find factorial of a number using recursion
For example : Suppose we are having 5 objects. Then
we can select 2 objects in C(5,2) = 10 i.e in 504 ways. combination can
be calculated by the
formula : C(n,r) = n!/((n-r)!r!) where
n = number of available objects
and
r = number of selected objects from the group.
C program for combination :
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int p,r,n;
printf(" Enter number for the calculating combination C(n,r)\n");
printf("Enter n value : ");
scanf("%d",&n);
printf("\nEnter r value : ");
scanf("%d",&r);
p=fact(n)/(fact(n-r)*fact(r));
printf("\nFactorial of C(%d,%d) = %d",n,r,p);
getch();
}
int fact(int c)
{
int f=1;
while(c>0)
{
f=f*c;
c--;
}
return f;
}
Output of C program for combination :
No comments:
Post a Comment