C language implementation of factorial permutation and combination of instances

  • 2020-04-02 03:09:58
  • OfStack

This article illustrates the factorial, permutation and combination of C language. Share with you for your reference. The details are as follows:


#include<stdio.h>
int Factorial(int n)
{
  int i=0;
  int sum=1;
  int array[]={0};
  for(i=n;i>=1;i--)
  {
    sum=sum*i;
  }
  return sum;
}
int Arrangement(int n,int m)
{
  int result=0;
  if(m>n)
  {
    printf(" There is something wrong with your input. The number above cannot be larger than the number below ! OK?");
    return 0;
  }
  if(m<n)
  {
    result=Factorial(n)/Factorial(n-m);
    return result; 
  }
}
int Combination(int n,int m)
{
  int result=0;
  if(m>n)
  {
    printf(" There is something wrong with your input. The number above cannot be larger than the number below ! OK?");
    return 0;
  }
  if(m<n)
  {
    int temp=0;
    temp=Factorial(n-m)*Factorial(m);
    result=Factorial(n)/temp;
    return result; 
  }
}
main()
{
  int res=0;
  res=Arrangement(5,2);
  printf("%d",res);
  system("pause");
}

Hope that the article described in the C programming language for you to help.


Related articles: