---
title: "Factorial Calculation"  
description: "Factorial Calculation"  
author: "Steilla Mitchel"  
published: 2024-06-11  
updated: 2024-06-14  
canonical: https://www.mindstick.com/forum/160723/factorial-calculation  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 3 minutes  

---

# Factorial Calculation

Create a [C# program](https://www.mindstick.com/forum/156830/how-do-i-make-a-c-sharp-program-that-checks-for-given-array-by-user-which-elements-are-perfect-numbers) to [calculate the factorial](https://www.mindstick.com/forum/158751/create-a-function-to-calculate-the-factorial-of-a-given-number-using-recursion) of a given non-negative [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) n. The factorial of a number is the [product](https://www.mindstick.com/articles/75385/full-product-keys) of all positive integers less than or equal to n.

## Replies

### Reply by Ashutosh Patel

#### C# Factorial Calculation

When calculating the factor of a number, any positive whole number less than that number must be multiplied by that number. The product of the number n (denoted as n!) is the product of all positive integers less than or equal to n.

## Syntax-

5! = 5 *4 * 3 * 2 * 1 = 120.

3! = 3 * 2 * 1 = 6.

There are two ways mentioned below to calculate the factorial of an integer,

#### Iterative Approach

## Example-

```cs
using System;
class FactorialCalculator
{
   static void Main()
   {
       Console.WriteLine("Enter a non-negative integer to calculate its factorial:");
       if (int.TryParse(Console.ReadLine(), out int number) && number >= 0)
       {
           Console.WriteLine("The factorial of {0} is {1}.", number, CalculateFactorial(number));
       }
       else
       {
           Console.WriteLine("Invalid input. Please enter a non-negative integer.");
       }
        Console.ReadLine();
   }
   // function to calculate factorial
   static long CalculateFactorial(int number)
   {
       long result = 1;
       for (int i = 1; i <= number; i++)
       {
           result *= i;
       }
       return result;
   }
}
```

## In the above example-

**Main method-** Asks the user to enter a non-negative integer and verifies the input using int.TryParse and verifies that the number is non-negative. If true, calculate the factor using the CalculateFactorial method.

**CalculateFactorial Method-** Uses a recursive method with a for loop to calculate a factorial by multiplying the result by any integer from 1 to a given number.

## Output-

```plaintext
Enter a non-negative integer to calculate its factorial:
5
The factorial of 5 is 120.
```

#### Recursive Approach

Here is an alternative approach for calculate the factorial of a integer for educational purpose,

## Example-

```cs
using System;
class FactorialCalculator
{
   static void Main()
   {
       Console.WriteLine("Enter a non-negative integer to calculate its factorial:");
       if (int.TryParse(Console.ReadLine(), out int number) && number >= 0)
       {
           Console.WriteLine("The factorial of {0} is {1}.", number, CalculateFactorial(number));
       }
       else
       {
           Console.WriteLine("Invalid input. Please enter a non-negative integer.");
       }
        Console.ReadLine();
   }
   static long CalculateFactorial(int number)
   {
       if (number <= 1)
       {
           return 1;
       }
       return number * CalculateFactorial(number - 1);
   }
}
```

**In the above example-**\
**Main method-** Same as the general method.

**CalculateFactorial Method-** Uses recursion to calculate factorial. The base case occurs when the number is 1 or less, in which case it returns 1. Otherwise, it calls itself with the number - 1 and multiplies the result by the number.

## Output-

```plaintext
Enter a non-negative integer to calculate its factorial:
6
The factorial of 6 is 720.
```

Either approach is correct, but recursive methods are generally more efficient in terms of memory usage because recursive procedures can generate excess stack for very small values ​​of the quantity.

**Also, Read:** [Palindrome Checker](https://www.mindstick.com/forum/160724/palindrome-checker)


---

Original Source: https://www.mindstick.com/forum/160723/factorial-calculation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
