Introduction
In this blog, we will write a C# program to user-define a factorial number.
User-Define Factorial number in C#
This code sample shows you how to user-define a factorial number in C#:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace UserDefineFactorialNumber {
- class Program {
- static void Main(string[] args) {
- int a = 1, Fact = 1;
- int Num = 5;
- while (a <= Num) {
- Fact = Fact * a;
- a++;
- Console.WriteLine("The Factorial Number is " + Fact);
- }
- Console.ReadLine();
- }
- }
- }
Now run the user-defined factorial number code
Click on the Start button:

Output of the code:

I hope you understood how to user-define a factorial number using C# code.

Jacek MarchelPosted Oct 2, 2020, 9:45 AM
The issue with the program is that factorial grows very quickly. Using 32 bit integer for calculations will overflow already at 13!. Using long 64 bit integer will overflow at 21!. So the program should use checked keyword for monitoring overflow and catch the exception. For memory limited size the program should use BigInteger structure from .NET that uses unlimited precision integers.