Friday, July 10, 2015

factorial by function

#include <stdio.h>

int factorial(int n);

int main()
{
  int fact;


  printf("Please enter a number:");
  scanf("%d", &fact);

  printf("The factorial of %d is:%d",fact, factorial(fact));

  return 0;
}

int factorial(int n)
{
  int i;
int factorial = 1;

  for (i = 1; i<= n; i++)
    factorial = factorial * i;

  return factorial;
}


output:
                                                   

No comments:

Post a Comment