C# Sharp Exercises: Find the factorial of a given number
C# Sharp Recursion: Exercise-9 with Solution
Write a program in C# Sharp to find the factorial of a given number using recursion.
Pictorial Presentation:
Sample Solution:
C# Sharp Code:
using System;
class RecExercise9
{
static void Main(string[] args)
{
Console.WriteLine("\n\n Recursion : Find the factorial of a given number :");
Console.WriteLine("-------------------------------------------------------");
Console.Write(" Input any positive number : ");
int n1 = Convert.ToInt32(Console.ReadLine());
long fact = FactorialCalcu(n1);
Console.WriteLine(" The factorial of {0} is : {1} ", n1, fact);
Console.ReadKey();
}
private static long FactorialCalcu(int n1)
{
if (n1 == 0)
{
return 1;
}
return n1 * FactorialCalcu(n1-1);
}
}
Sample Output:
Recursion : Find the factorial of a given number : ------------------------------------------------------- Input any positive number : 8 The factorial of 8 is : 40320
Flowchart :
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a program in C# Sharp to Check whether a given String is Palindrome or not using recursion.
Next: Write a program in C# Sharp to find the Fibonacci numbers for a n numbers of series using recursion.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework