0
0
CsharpProgramBeginner · 2 min read

C# Program to Find Factorial of a Number

You can find the factorial of a number in C# by using a loop to multiply numbers from 1 to that number, like for (int i = 1; i <= n; i++) factorial *= i;.
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input10
OutputFactorial of 10 is 3628800
🧠

How to Think About It

To find the factorial of a number, multiply all whole numbers from 1 up to that number. Start with 1 because multiplying by 1 does not change the value. Keep multiplying each next number until you reach the input number.
📐

Algorithm

1
Get the input number n
2
Set factorial to 1
3
For each number i from 1 to n, multiply factorial by i
4
After the loop ends, return or print factorial
💻

Code

csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int n = int.Parse(Console.ReadLine());
        long factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        Console.WriteLine($"Factorial of {n} is {factorial}");
    }
}
Output
Enter a number: 5 Factorial of 5 is 120
🔍

Dry Run

Let's trace the input 5 through the code

1

Input

User enters 5

2

Initialize factorial

factorial = 1

3

Loop i=1

factorial = 1 * 1 = 1

4

Loop i=2

factorial = 1 * 2 = 2

5

Loop i=3

factorial = 2 * 3 = 6

6

Loop i=4

factorial = 6 * 4 = 24

7

Loop i=5

factorial = 24 * 5 = 120

8

End loop

Print factorial = 120

ifactorial
11
22
36
424
5120
💡

Why This Works

Step 1: Start with 1

We start factorial at 1 because multiplying by 1 keeps the value unchanged and is the base for multiplication.

Step 2: Multiply in loop

Each loop step multiplies the current factorial by the next number using factorial *= i to build the product.

Step 3: Result after loop

After the loop finishes, factorial holds the product of all numbers from 1 to n, which is the factorial.

🔄

Alternative Approaches

Recursive method
csharp
using System;
class Program {
    static long Factorial(int n) {
        if (n <= 1) return 1;
        return n * Factorial(n - 1);
    }
    static void Main() {
        Console.Write("Enter a number: ");
        int n = int.Parse(Console.ReadLine());
        Console.WriteLine($"Factorial of {n} is {Factorial(n)}");
    }
}
Uses recursion which is elegant but can cause stack overflow for very large n.
Using LINQ Aggregate
csharp
using System;
using System.Linq;
class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int n = int.Parse(Console.ReadLine());
        long factorial = n == 0 ? 1 : Enumerable.Range(1, n).Aggregate((a, b) => a * b);
        Console.WriteLine($"Factorial of {n} is {factorial}");
    }
}
Uses LINQ for a functional style; less explicit but concise.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs from 1 to n, so the time grows linearly with n, making it O(n).

Space Complexity

Only a few variables are used, so space is constant O(1).

Which Approach is Fastest?

The iterative loop is fastest and safest; recursion adds overhead and risk of stack overflow; LINQ is concise but slightly slower.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)General use, large inputs
Recursive methodO(n)O(n)Simple code, small inputs
LINQ AggregateO(n)O(1)Concise code, readability
💡
Use a long type for factorial to handle larger numbers without overflow.
⚠️
Forgetting that factorial of 0 is 1 and returning 0 instead.