Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Calculate the Factorial of an Integer in C#

2.25/5 (6 votes)
3 Oct 2011CPOL 18.6K  
Use Recursion.int factorial(int n){ if(n<0) return -1; if(n==0 || n==1) return 1; return n * factorial(n-1);}
Use Recursion.
C#
int factorial(int n)
{
 if(n<0)
  return -1;
 if(n==0 || n==1)
  return 1;

 return n * factorial(n-1);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)