24+ C# Factorial Calculator Examples

This post contains a total of 24+ Hand-Picked C# Factorial Calculator Examples with Source Code. All the Factorial Calculators are made using C Sharp Programming language.

You can use the source code of these programs for educational purpose with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Cody Sharp

Made by Cody Sharp. This is a simple C# factorial calculator based on array. In the INPUT, enter the number for which you want to get factorial. Then the program displays answer in the console. ( Source )

using System;

namespace SoloLearn
{
    class FactorialCalc
    {
        public static double GetFactorial(int num)
        {
            double factorialNum = 1;
            int[] numRange = new int[num];
            for(int i = 0; i < numRange.Length; i++, num--)
                numRange[i] = num;
            foreach(int j in numRange) factorialNum *= (double)j;
            return factorialNum;
        }
        static void Main(string[] args)
        {   
            int num = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine($"The value of factorial of {num} is: " + GetFactorial(num));
        }
    }
}

2. By dynasteve16

Made by dynasteve16. It calculates the factorials of numbers. ( Source )


using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace dynasteve16
{

    public class OnWards 
    {
    	static int fact(int num){
    		
    		if(num == 1){
    			return 1;
    		}else{
    			return num * fact(num - 1);
    		}
    	}
        public static void Main(string[] args){
            Console.WriteLine("Input your number to calculate factorial");
            
            int x = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(fact(x));
            
            Console.WriteLine("THANKS PLEASE LIKE AND FOLLOW ME");
        }
    }
}

3. By Cardstdani

Made by Cardstdani. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            long a;
            long b;
            long c = 0;
            
            Console.WriteLine("The number is: ");
            a = Convert.ToInt32(Console.ReadLine());
            
            b = a;
            
            for(int i = 1; i < a;i++)
            {
                b *= i;
            }
            
            Console.WriteLine(b);
        }
    }
}

4. By Bart

Made by Bart. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static int Fact(int num) 
        {
            if (num == 1)
            {
                return 1;
            }
            if (num != 2)
                Console.Write(num+" * ");
            else
                Console.Write(num+" * 1 = ");
            return num * Fact(num - 1);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Give us a number to factorial");
            int x = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Factorial: ");
            Console.WriteLine(Fact(x));
        }
    }
}

5. By William

Made by William. Factorial calculator in C#. Will not go over 33! for 32 bit limitations. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
	class Program
	{
		static void Main(string[] args) {
		    int input;
		    input = Convert.ToInt32(Console.ReadLine());
		    
		    Console.WriteLine(Factorial(input));
		}
		
		
	   static int Factorial(int n) {
	        int Fact = 1;
	        
	        for(int i = 1; i <= n; i++) {
	           Fact *= i;
	        }
	        return Fact;
	   }
	}
}

6. By Kamlesh Kumar

Made by Kamlesh Kumar. ( Source )

using System;  
  public class Factorial 
   {  
     public static void Main(string[] args)  
      {  
       int i,fact=1,number;      
       Console.Write("Enter any Number: ");      
       number= int.Parse(Console.ReadLine());     
       for(i=1;i<=number;i++){      
        fact=fact*i;      
       }      
       Console.Write("Factorial of " +number+" is: "+fact);    
     }  
  }  

7. By Poker 64

Made by Poker 64. Simple C# program to get the factorial of a number. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number to calculate his factorial");
            int x_input = Convert.ToInt32(Console.ReadLine());
            int x = x_input;
            int x_fact = 1;
            
            if (x_input == 0) 
            {
                Console.WriteLine("0! = 1");
            }
            else if (x_input < 0)
            {
                x_input *= -1;
                x = x_input;
                for ( ; x != 0; x--)
                {
                    x_fact *= x;
                }
                Console.WriteLine("-{0}! = -{1}", x_input, x_fact);
            }
            else
            {
                for ( ; x != 0; x--)
                {
                    x_fact *= x;
                }
                Console.WriteLine("{0}! = {1}", x_input, x_fact);
            }
            
        }
    }
}

8. By Danil

Made by Danil. This little program can calculate factorials. Input any integer number from 0 to 20. Typing in any number lesser then 0 or higher then 20 will cause an error message to be displayed. Same will happen if you input an incorrect data type. ( Source )


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
         try{
         long x = Convert.ToInt64(Console.ReadLine());
         long num = 1;
         long res = 1;
         if(x<0||x>20){
            Console.WriteLine("Invalid input. Please type in an integer number from 0 to 20."); 
         }else{
         while(num<x){
            res *= ++num;
        }Console.WriteLine("{0}!={1}",x, res);}
        }catch (Exception e){
         Console.WriteLine("Invalid input. Please type in an integer number from 0 to 20.");   
        } 
        }
    }
}

9. By Natan

Made by Natan. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
	class Program
	{
		static int Fact(int num) {
			if (num == 1) {
				return 1;
			}
			return num * Fact(num - 1);
		}
		static void Main(string[] args)
		{
		int input = Convert.ToInt32(Console.ReadLine());
			Console.WriteLine(Fact(input));
		}
	}
}

10. By Samuel

Made by Samuel. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int factorial;
            int number;
            int i;
            Console.WriteLine("Enter Number");
            number = Convert.ToInt32(Console.ReadLine());
            factorial =1;
            for(i = 1 ; i <= number; i++)
            {
               factorial = factorial *i; 
            }
            Console.WriteLine("Factorial is:" + factorial);
            Console.ReadLine();
        }
    }
}

11. By Cody Sharp

Made by Cody Sharp. Factorial calculator using recursive method. ( Source )


using System;

namespace SoloLearn{
	class Program
	{
		static void Main(string[] args)
		{
		  int number = Console.ReadLine();
		  Console.WriteLine("Factorial of {0} is {1}.", number, Factorial(number));
		}
		
		static int Factorial(int number) 
		{
			if(x == 0) return 1;
			return number * Factorial(number - 1)
		}
	}
}

12. By Kirby442

Made by Kirby442. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = Convert.ToInt32(Console.ReadLine());
            Fact(num);
            Console.WriteLine("{0}! = {1}",num,Fact(num));
        }
        static int Fact(int num) {
            if(num == 1) {
                return 1;
            }
            return num * Fact(num - 1);
        }
    }
}

13. By GenyoNguyen

Made by GenyoNguyen. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
	class Program
	{
	    static int Fact(int x)
	    {
	        if(x == 1)
	        {
	        return 1;
	        }
	        return x*Fact(x - 1);
	    }
	   
	    
        static void Main(String[] args)
        {
           
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Fact(a);
            Console.WriteLine(b);
        }
		
		
	
	}
}

14. By Wojciech Powch

Made by Wojciech Powch. Enter positive integer number to get the factorial of that number. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
	class Program
	{
		static void Main(string[] args)
		{
		    int number_fact = 1;
		    int number = int.Parse(System.Console.ReadLine());
			for(int i = 2;i<=number;i++ ){
			    number_fact = number_fact * i;
			}
			Console.WriteLine("Factorial of number {0} is {1}",number,number_fact);
			
		}
	}
}

15. By Marko Stankovic

Made by Marko Stankovic. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Sololearn
{
    class Program
    {
        static int num;
        static int factorial;
        
        static int Fact(int a)
        {
            int result = 1;
            //the factorial of 0 is 1
            if (a == 0)
            {
                return result;
            }
            
            //if the number is not zero, result is multiplied by 1, then 2, then 3..etc until it reaches the number you input
            for (int i = 1; i <= a; i++)
            {
                result *= i;
            }
            return result;
        }
        
        static void Main(string[] args)
        {
            //you input a number
            num = int.Parse(Console.ReadLine());
            //the factorial of it is calculated using the Fact method declared at the beggining
            factorial = Fact(num);
            //the result is printed
            Console.WriteLine("The factorial of " + num + " is " + factorial);
        }
    }
}

16. By Vacid

Made by Vacid. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int num=0;
            int result  =1;
            
Console.WriteLine("enter a number: ");
num=Convert.ToInt16(Console.ReadLine());
          Console.Write(num+"!=");
           Console.Write(num);
           
          for(int i=num;i>1;i--)
            {
              Console.Write("Γ—"+(i-1));
                result = result*i ; 
                
            }
            
            Console.Write("="+result);
           
        }
    }
}

17. By Karl Michael

Made by Karl Michael. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
	class Program
	{
		static void Main(string[] args)
		{
		    Console.WriteLine("Enter a number: ");
		    int num = Convert.ToInt32(Console.ReadLine());
		    Console.WriteLine(Factorial(num));	
		}
		
    	private static int Factorial(int arg)
    	{
    	    int ret = 1;
    	    for(int i = arg; i > 1; i--)
    	    {
    	        ret *= i;
    	    }
    	    return ret;
    	}
	}
}

18. By NezhnyjVampir

Made by NezhnyjVampir. A 64 bit factorial finder in C#. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static long Factorial (long n) {
            if (n == 1 || n == 0) {
                return 1;
            }
            return n * Factorial (n - 1);
        }
        static void Main(string[] args)
        {
            long num = Convert.ToInt64(Console.ReadLine());
            Console.WriteLine(Factorial (num));
        }
    }
}

19. By NezhnyjVampir

Made by NezhnyjVampir. A 32 bit factorial finder in C#. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static int Factorial (int n) {
            if (n == 1 || n == 0) {
                return 1;
            }
            return n * Factorial (n - 1);
        }
        static void Main(string[] args)
        {
            int num = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(Factorial (num));
        }
    }
}

20. By VEDANG

Made by VEDANG. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using @XPL;

namespace @XPL
{
    class @Text
    {
        public static void @Set(string x)
        {
            Console.WriteLine(x);
        }
        public static string @Get()
        {
            return Console.ReadLine();
        }
  public static double @ToDouble(object x)
        {
            return Convert.ToDouble(x);
        }
    }
}

namespace @Made_by_Vedang
{
    class @Code
    {
        static void Main(string[] args)
        {
             
            double x;
            double y;
            Text.Set("Enter the Number : ");
            var st =Text.Get();
        try{
            x = Text.ToDouble(st);
            y = x;
       for (double i = x - 1; i >= 1; i--)
            {
                y *= i;
            }
    Text.Set("\nFactorial of Given Number is: " + y);
            }
         catch (FormatException){
            Text.Set("Invalid input");
            }
        }
    }
}

21. By Sololearn

Made by Sololearn. C# Factorial calculator using recursion. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/* the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. */
namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            //sample number
            int n = 8;
            
            int r = Factorial(n);
            Console.WriteLine(n + "! = " + r);
        }
        static int Factorial(int n)
        {
            if (n <= 1)
                return 1;
                
            return n * Factorial(n - 1);
        }
    }
}

22. By Dominik ChmelΓ­k

Made by Dominik ChmelΓ­k. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
	class Program
	{
		static void Main(string[] args)
		{
			try
			{
				ulong input = ulong.Parse(Console.ReadLine());
				Console.WriteLine("The result of {0}! is {1}.", input, Factorial(input));
			}
			catch
			{
				Console.WriteLine("Input value is invalid!!!, Please, make sure that input value is number higher or equal 0!!!");
			}
		}
		
		static ulong Factorial(ulong i)
		{
			if(i <= 1)
			   return 1;
		
			return i * Factorial(i - 1);
		}
	}
}

23. By Yididiya

Made by Yididiya. Factorial finder using array and for loop. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Sololearn
{
    class Program
    {
        static void Main(string[] args)
        {
        try{
        
            int x = Convert.ToInt32(Console.ReadLine());
            
            int[] arr = new int[x];
            
            int fl = 1;
            
            for (int i = 0; i < x; i++){
            
                arr[i] = (i+1);
                
            }
            foreach (int k in arr){
            
            fl *= k;
            
                
            }
            Console.WriteLine("Factorial of {0} is {1}.", x, fl);
        }
        catch(Exception e){
            Console.WriteLine("Please enter a whole number!");
        }
        }
    }
}

24. By πŸ‘‘ Prometheus πŸ‡ΈπŸ‡¬

Made by πŸ‘‘ Prometheus πŸ‡ΈπŸ‡¬. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int b=5;
            int a=1;
            for (int i=1;i<=b;i++){
                a*=i;
            }
            Console.Write("The factorial of the value b is: ");
            Console.Write(a);
        }
    }
}

25. By Chirag Bhansali

Made by Chirag Bhansali. C# Program to Generate the Factorial of Given Number. ( Source )


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, number, fact;
            Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            fact = number;
            for (i = number - 1; i >= 1; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine("\nFactorial of Given Number is: "+fact);
            Console.ReadLine();
 
        }
    }
}