Thursday 26 May 2016

Permutations and Combinations #1 : C program.

Hello friends !
Today we are going to deal with extremely powerful tool of 'C' language , and that is user-defined "Functions".
Some prerequisite knowledge is required to understand this program, and that is -

  1. Switch case:
  2. Functions 
  3. Mathematical meaning of Permutations and Combination.
Basically , Permutation is the number of possible arrangements and Combination is the number of possible selections in a given case and under certain constraints .
Here we are going to calculate simply , nPr and nCr. 
We have certain mathematical formulas for calculating this -
  • nPr = n!/r! .
  • nCr = n!/(r!*(n-r)!) .
So , what we need to do is , make three functions for calculating 1.Factorial 2.Permutations & 3.Combinations .

* For finding factorial we can initiate fact=1, and run a loop using counter variable i n times performing the calculation fact=i*fact and increment i by 1 in each loop.
* And for finding nPr and nCr all we need to do is call function of factorial and put the required in factorial in the given formula .
And that's it !
Try to code yourself or copy the program given below , and check whether it is running !


C Program : Permutations and Combinations !



#include < stdio.h > 
/*program to calculate permutations and combinations !*/
int comb();/*function for calculating combination*/
int per();/*function for calculating permutation*/
int fact();/*function for calculating factorial*/

int main()
{
    int e,n,r,i,c,p;
    printf("Enter \n1. Permutations\n2. Combinations !\n");
    scanf("%d",&e);
    printf("Enter n and r\n");
    scanf("%d%d",&n,&r);
    switch(e)
    {
    case 1:
        p=per(n,r);/*calling a function for calculating permutation */
        /* take note that values within the parenthesis are assigned to parameters of the function*/
        printf("nPr=%d\n",p);/*printing the answer*/
        break;/*break is essential while using Switch-case after every step*/
        /* If break is not used then the all following cases will be calculated*/
    case 2:
        c=comb(n,r);
        printf("nCr=%d\n",c);
        break;
    default :
        printf("bye!");
    }

}
int per(int x,int y)
{
    int a,b,c;
    a=fact(x);
    b=fact(y);
    c=a/b;/* according to formula of nPr */
    return(c);/*returning the calculated value of nPr*/
}
int comb(int x,int y)
{
    int a,b,c,d;
    a=fact(x);
    b=fact(y);
    c=fact(x-y);
    d=a/(b*c);/*according to formula of nCr*/
    return(d);/*returning the calculated value of nCr*/
}
int fact(int x)
{
    int i,p=1;
    for(i=1;i < = x ; i++)
    {
        p=p*i;/* multiplying 1*2*3*...n times to find factorial*/
    }
    return(p);/* Returning value of calculated factorial*/
}


"Thanks for referring this , any modification and suggestions are accepted. "

5 comments:

  1. Use pointer as much as possible it reduces your code..

    ReplyDelete
    Replies
    1. Ok. But was following a sequence to publish articles thats why didnt used it.
      Never mind thanks !
      You too can help to way out by posting the whole program☺ ..

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This comment has been removed by the author.

      Delete