Thursday 3 October 2013

Finding Permutation and Combination of numbers using C

#include<stdio.h>
long factorial(int);
long combination(int, int);
long permutation(int, int);
main()
{
   int n, r;
   long ncr, npr;
   printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
   printf("Enter the value of n and r\n");
   scanf("%d%d",&n,&r);
   ncr = find_ncr(n, r);
   npr = find_npr(n, r);
   printf("%dC%d = %ld\n", n, r, ncr);
   printf("%dP%d = %ld\n", n, r, npr);
   return 0;
}

long combination(int n, int r)
{
   long result;
   result = factorial(n)/(factorial(r)*factorial(n-r));
   return result;
}

long permutation(int n, int r)
{
   long result;
   result = factorial(n)/factorial(n-r);
   return result;
}

long factorial(int n)
{
   int c;
   long result = 1;
   for( c = 1 ; c <= n ; c++ )
      result = result*c;
   return ( result );
}

No comments:

Post a Comment