Three Different ways to calculate factorial in C#

The factorial of a number is the product of all positive integers less than or equal to that number. For example, factorial of 5 (written as 5!) is 5 × 4 × 3 × 2 × 1 = 120. In C#, you can calculate factorial using three different approaches −

Syntax

The mathematical definition of factorial is −

n! = n × (n-1) × (n-2) × ... × 2 × 1
0! = 1 (by definition)
1! = 1

Factorial Calculation Methods For Loop Iterative Downward While Loop Iterative Accumulative Recursion Self-calling Function All methods calculate: 5! = 5 × 4 × 3 × 2 × 1 = 120

Using For Loop

The for loop approach calculates factorial by multiplying numbers from the given value down to 1 −

Example

using System;

namespace factorial {
   class Test {
      static void Main(string[] args) {
         int i, res;
         int value = 5;
         res = value;
         for (i = value - 1; i >= 1; i--) {
            res = res * i;
         }
         Console.WriteLine("Factorial of " + value + " = " + res);
      }
   }
}

The output of the above code is −

Factorial of 5 = 120

Using While Loop

The while loop approach uses a method that continuously multiplies the result by the current number until it reaches 1 −

Example

using System;

namespace MyApplication {
   class Factorial {
      public int CalculateFactorial(int n) {
         int res = 1;
         while (n != 1) {
            res = res * n;
            n = n - 1;
         }
         return res;
      }
      static void Main(string[] args) {
         int value = 5;
         int ret;
         Factorial fact = new Factorial();
         ret = fact.CalculateFactorial(value);
         Console.WriteLine("Factorial of " + value + " = " + ret);
      }
   }
}

The output of the above code is −

Factorial of 5 = 120

Using Recursion

The recursive approach calls itself with a decremented value until it reaches the base case (n = 1) −

Example

using System;

namespace MyApplication {
   class Factorial {
      public int CalculateFactorial(int n) {
         if (n == 1)
            return 1;
         else
            return n * CalculateFactorial(n - 1);
      }
      static void Main(string[] args) {
         int value = 5;
         int ret;
         Factorial fact = new Factorial();
         ret = fact.CalculateFactorial(value);
         Console.WriteLine("Factorial of " + value + " = " + ret);
      }
   }
}

The output of the above code is −

Factorial of 5 = 120

Comparison

Method Time Complexity Space Complexity Best For
For Loop O(n) O(1) Simple, efficient for small to medium numbers
While Loop O(n) O(1) Object-oriented approach, reusable method
Recursion O(n) O(n) Mathematical elegance, educational purposes

Conclusion

All three methods effectively calculate factorial in C#. The for loop and while loop approaches are more memory-efficient with O(1) space complexity, while recursion provides a mathematically elegant solution but uses O(n) stack space. Choose the method based on your specific requirements and coding style preferences.

Updated on: 2026-03-17T07:04:35+05:30

904 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements