Compute Permutations and Combinations of a Given Set of Objects - C Data Structure

C examples for Data Structure:Algorithm

Description

Compute Permutations and Combinations of a Given Set of Objects

Demo Code

#include <stdio.h>

int fact(int);
int combination(int, int);
int permutation(int, int);

int main()/*from   ww w  . j  av a  2  s.co  m*/
{
       int counter = 5, result = 3, intC, p;

       intC = combination(counter, result);
       p = permutation(result, result);

       printf("\nCombinations : %d", intC);
       printf("\nPermutations : %d", p);
       fflush(stdin);

       return 0;
}

int combination(int counter, int result)
{
    int intC;
    intC = fact(counter) / (fact(result) * fact(counter - result));
    return intC;
}

int permutation(int counter, int result)
{
    int p;
    p = fact(counter) / fact(counter - result);
    return p;
}

int fact(int counter)
{
    int i;
    int facto = 1;
    for (i = 1; i <= counter; i++)
    {
        facto = facto * i;
    }
    return facto;
}

Result


Related Tutorials