Example 1: C# program to find the factorial of a given number
Program.cs
longn,f=1;Console.WriteLine("———————————————————————————————————————————");Console.WriteLine("Program to find the factorial of a number");Console.WriteLine("———————————————————————————————————————————");Console.Write("Enter the number ");n=long.Parse(Console.ReadLine()!);if(n==0){Console.WriteLine("\nFactorial of 0 is 1");}elseif(n>=21){Console.WriteLine("Number should be less than 21");}else{f=n;for(longi=n-1;i>=2;i--){f*=i;}Console.WriteLine("\nFactorial of {0} is {1}",n,f);}Console.WriteLine("———————————————————————————————————————————");
Output
kodingwindow@kw:~/csharp$ dotnet run
———————————————————————————————————————————
Program to find the factorial of a number
———————————————————————————————————————————
Enter the number 16
Factorial of 16 is 20922789888000
———————————————————————————————————————————
kodingwindow@kw:~/csharp$
Example 2: C# program to find the factorial of a given number
Program.cs
longn,f=1;Console.WriteLine("———————————————————————————————————————————");Console.WriteLine("Program to find the factorial of a number");Console.WriteLine("———————————————————————————————————————————");Console.Write("Enter the number ");n=long.Parse(Console.ReadLine()!);if(n<0){Console.WriteLine("\nPlease enter a positive number");}else{while(n>0){f*=n;n--;}Console.WriteLine("\nFactorial of {0} is {1}",n,f);}Console.WriteLine("———————————————————————————————————————————");
Output
kodingwindow@kw:~/csharp$ dotnet run
———————————————————————————————————————————
Program to find the factorial of a number
———————————————————————————————————————————
Enter the number 0
Factorial of 0 is 1
———————————————————————————————————————————
kodingwindow@kw:~/csharp$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.