Updated July 10, 2023

## Introduction to Factorial in C#

In this section, we shall see the factorial in c# in detail. Factorial is a very important concept in the area of mathematics like in algebra or in mathematics analytics. It is denoted by sign of exclamation (!). Factorial is any positive integer k, which is denoted by k! It is the product of all positive integers which are less than or equal to k.

k!= k * (k-1) *(k-2) *(k-3) *(k-4) *…….3 *2 * 1.

**Logic to Calculate Factorial of A Given Number**

For example, if we want to calculate the factorial of 4 then it would be,

**Example #1**

4! = 4 * (4-1) *(4-2) * (4-3)

4! = 4 * 3 * 2 * 1

4! = 24.

So factorial of 4 is 24

**Example #2**

6! = 6 * (6-1)* (6-2)* (6-3) * 6-4)* (6-5)

6! = 6*5*4*3*2*1

6! = 720

So factorial of 6 is 720

Similarly, by using this technique we can calculate the factorial of any positive integer. The important point here is that the factorial of 0 is 1.

0! =1.

There are many explanations for this like for n! where n=0 signifies product of no numbers and it is equal to the multiplicative entity. {\displaystyle {\binom {0}{0}}={\frac {0!}{0!0!}}=1.}

The factorial function is mostly used to calculate the permutations and combinations and also used in binomial. With the help of the factorial function, we can also calculate the probability. For example in how many ways we can arrange k items. We have k choices for the first thing, So for each of these k choices, we left with k-1 choices for the second things (because first choice has already been made), so that now we have k(k-1) choices, so now for the third choice we have k(k-1)(k-2) choices and so on until we get one on thing is remaining. So altogether we will have k(k-1)(k-2)(k-3)…3..1.

Another real-time example is supposed we are going to a wedding and we want to choose which blazer to take. So let’s suppose we have k blazers and but have room to pack the only n. So how many ways we can use n blazers from a collection of k blazers k!/(n!.(k-n)!).

### Examples of Factorial in C#

Below are the examples to show how we can calculate factorial of any number in different ways,

#### Example #1

1. In these examples, for loop is used to calculate the factorial of a number.

**Code:**

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Factorial
{
class Program
{
static void Main()
{
int a = 7;
int fact = 1;
for (int x = 1; x <= a; x++)
{
fact *= x;
}
Console.WriteLine(fact);
Console.ReadLine();
}
}
}
```

In this example, the variable of integer data type is initialized and for loop is used to calculate the number.

**Output:**

2. In this example, the user is allowed to enter the number to calculate the factorial.

**Code:**

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactorialExample
{
class Program
{
static void Main()
{
Console.WriteLine("Enter the number: ");
int a = int.Parse(Console.ReadLine());
int fact = 1;
for (int x = 1; x <= a; x++)
{
fact *= x;
}
Console.WriteLine(fact);
Console.ReadLine();
}
}
}
```

**Output:**

#### Example #2

1. In these examples, for loop is used to calculate the factorial of a number.

**Code:**

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Factorial
{
class Program
{
static void Main()
{
int a = 10;
int fact = 1;
while (true)
{
Console.Write(a);
if (a == 1)
{
break;
}
Console.Write("*");
fact *= a;
a--;
}
Console.WriteLine(" = {0}", fact);
Console.ReadLine();
}
}
}
```

**Output:**

2. In these examples, while loop is used to calculate the factorial of a number.

**Code:**

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactorialExample
{
class Program
{
static void Main()
{
Console.WriteLine("Enter the number: ");
int a = int.Parse(Console.ReadLine());
int fact = 1;
while(true)
{
Console.Write(a);
if(a==1)
{
break;
}
Console.Write("*");
fact *= a;
a--;
}
Console.WriteLine(" = {0}", fact);
Console.ReadLine();
}
}
}
```

**Output:**

#### Example #3

1. In this example, do-while is used to calculate the factorial of a number.

**Code:**

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Factorial
{
class Program
{
static void Main()
{
int a = 6;
int fact = 1;
do
{
fact *= a;
a--;
} while (a > 0);
Console.WriteLine("Factorial = {0}", fact);
Console.ReadLine();
}
}
}
```

**Output:**

2. In this example, do-while is used to calculate the factorial of a number.

**Code:**

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactorialExample
{
class Program
{
static void Main()
{
Console.Write("Enter the number: ");
int a = int.Parse(Console.ReadLine());
int fact = 1;
do
{
fact *= a;
a--;
} while (a > 0);
Console.WriteLine("Factorial = {0}", fact);
Console.ReadLine();
}
}
}
```

**Output:**

#### Example #4

1. In this example, a recursive function is used to calculate the factorial of a number.

**Code:**

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Factorial
{
class Program
{
static void Main()
{
int n= 5;
long fact = Fact(n);
Console.WriteLine("factorial is {1}", n, fact);
Console.ReadKey();
}
private static long Fact(int n)
{
if (n == 0)
{
return 1;
}
return n * Fact(n - 1);
}
}
}
```

In the above example, the factorial of a number is achieved by using recursion. The idea behind the recursion is to solve the problem in small instances. So whenever a function creating a loop and calling itself, it’s called recursion.

**Output:**

2. In this example, a recursive function is used to calculate the factorial of a number.

**Code:**

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactorialExample
{
class Program
{
static void Main()
{
Console.WriteLine("Enter the number");
int n = Convert.ToInt32(Console.ReadLine());
long fact = Fact(n);
Console.WriteLine("factorial is {1}", n, fact);
Console.ReadKey();
}
private static long Fact(int n)
{
if (n == 0)
{
return 1;
}
return n * Fact(n - 1);
}
}
}
```

**Output:**

### Conclusion

So the concept of factorial is very important in areas of mathematics such as binomials and permutations and combinations, and this is how we can print the factorial of any number by using multiple methods such as for, while, do-while, function, etc.

### Recommended Articles

This is a guide to Factorial in C#. Here we discuss the basic concept of factorial in c# along with different examples and code implementation. You may also look at the following articles to learn more –