Face Recognition Using Microsoft Vision API
Factorial Calculator using C# July 1, 2018 Mahedee Fb-Button
Factorial is a positive number which is the product of all number less than or equal to that positive number. For example:
0! = 1
1! = 1
2! = 2 * 1! = 2
3! = 3 * 2! = 6
…
n! = n * (n – 1)!
Here is the sample code to calculate Factorial using both loop and recursion using c#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Factorial01
{
/// <summary>
/// Author: Md. Mahedee Hasan
/// </summary>
class Program
{
static void Main(string[] args)
{
int number = 0;
Console.WriteLine("Enter an integer number to calculate factorial: ");
Int32.TryParse(Console.ReadLine(), out number);
Console.WriteLine("Factorial of " + number + " is (using loop): " + CalculateFactoial(number));
Console.WriteLine("Factorial of " + number + " is (Using recursion): " + Factorial(number));
Console.ReadLine();
}
//Calculate factorial using loop
private static int CalculateFactoial(int number)
{
int factorial = 1;
for (int counter = 1; counter <= number; counter++)
{
factorial = factorial * counter;
}
return factorial;
}
//Calculate factorial using recursion
private static int Factorial(int number)
{
if (number == 0)
return 1;
else
return number * Factorial(number - 1);
}
}
}