The factorial of a number is a common mathematical operation often used in combinatorics, probability, and algorithm challenges. In this article, we'll learn how to calculate factorials in C# using different methods and handle various input cases.
The factorial of a non-negative integer 𝑛 n is the product of all positive integers less than or equal t 𝑛. It is denoted by 𝑛!.
Formula n!=n×(n−1)×(n−2)×…×2×1  
 5!=5×4×3×2×1=120  
 3!=3×2×1=6  
 1!=1  
 0!=1  
Calculating The Factorial Of A Number
Calculating the factorial of a number in C# can be done using either an iterative or a recursive approach. Here are examples of both methods:
Iterative Approach
This is the most straightforward and efficient approach for calculating factorials.
 using System;  
 class Program  
 {  
   static void Main()  
   {  
     Console.Write("Enter a number: ");  
     int number = int.Parse(Console.ReadLine());  
     long factorial = Factorial(number);  
     Console.WriteLine($"Factorial of {number} is {factorial}");  
   }  
   static long Factorial(int n)  
   {  
     long result = 1;  
     for (int i = 1; i <= n; i++)  
     {  
       result *= i;  
     }  
     return result;  
   }  
 }  
Recursive Approach
Factorials have a natural recursive structure, so recursion is a good fit for this problem.
 using System;  
 class Program  
 {  
   static void Main()  
   {  
     Console.Write("Enter a number: ");  
     int number = int.Parse(Console.ReadLine());  
     long factorial = Factorial(number);  
     Console.WriteLine($"Factorial of {number} is {factorial}");  // Result of factoriale
   }  
   static long Factorial(int n)  
   {  
     if (n == 0 || n == 1)  
       return 1;  
     return n * Factorial(n - 1);  
   }  
 }  
You can run either of these programs in a C# environment. Just input a non-negative integer, and it will calculate the factorial. Keep in mind that for larger values of n, the result can exceed the range of long, so consider using BigInteger for very large factorials:
 using System;  
 using System.Numerics;  
 class Program  
 {  
   static void Main()  
   {  
     Console.Write("Enter a number: ");  
     int number = int.Parse(Console.ReadLine());  
     BigInteger factorial = Factorial(number);  
     Console.WriteLine($"Factorial of {number} is {factorial}");  
   }  
   static BigInteger Factorial(int n)  
   {  
     if (n == 0 || n == 1)  
       return 1;  
     return n * Factorial(n - 1);  
   }  
 }  
Explanation
- Iterative Approach: Uses a loop to multiply numbers from 1 to n.
- Recursive Approach: Calls itself with decremented values until it reaches the base case.
Usage of Factorial Number
The factorial of a number, written as n!, has many important applications across mathematics, computer science, statistics, and real-world problem-solving. Here's an overview of the key uses of factorials:
- Combinatorics (Counting And Arrangements)
- Probability and Statistics
- Mathematics and Algebra
- Algorithm Design and Analysis
- Mathematical Functions and Sequences
Thanks