Factorial of a positive number is a number which is obtained by multiplying all sequence of number starting with 1 till the number itself. Thus factorial calculation is a repititive process and recursion is often used for this.

unsigned int factorial(unsigned int n )
{
  if ( n <= 1 )
  {
    return 1;
  }
  else
  {
    return n * factorial(n - 1);/*Call itself*/
  }
}

factorial can also be done using normal loop based logics. Below is non-recursive code sniffet.

unsigned int factorial(unsigned int n )
{
  int result = 1;
  for (i = n; i > 1; i--)
  {
    result = result * i;
  }
  return result;
}

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#