Click here to Skip to main content
15,120,326 members
Articles / Programming Languages / C#
Alternative
Tip/Trick
Posted 3 Oct 2011

Tagged as

Stats

15K views

Calculate the Factorial of an Integer in C#

Rate me:
Please Sign up or sign in to vote.
2.25/5 (6 votes)
3 Oct 2011CPOL
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)

Share

About the Author

Anshul R
Student
India India
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 good Pin
mandar130519-Oct-11 1:25
Membermandar130519-Oct-11 1:25 
Reason for my vote of 5
good
GeneralGuys, please. This is a tail-recursion and it won't be any s... Pin
ckbz18-Oct-11 22:13
Memberckbz18-Oct-11 22:13 
GeneralReason for my vote of 1 Besides, recursion for the sake of r... Pin
victorbos18-Oct-11 4:23
Membervictorbos18-Oct-11 4:23 
GeneralReason for my vote of 1 The time has long since passed when ... Pin
Chris Maunder4-Oct-11 3:47
cofounderChris Maunder4-Oct-11 3:47 
GeneralA standard and simple solution, my 5. However, the original ... Pin
Ed Nutting3-Oct-11 21:24
MemberEd Nutting3-Oct-11 21:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.