Generate the Factorial of Given Number This is a console application that generates the factorial of a given number. Is there any way I can improve my code ?

        int number = Convert.ToInt32(Console.ReadLine());
        int digitFactorial = number;
        for (int counter = number - 1; counter &gt; 0; counter--)
        {
            digitFactorial *= counter;
        }
        Console.WriteLine(digitFactorial);
        Console.ReadLine();
As Slicer wrote in his comment you should create a method/function for the algorithm.
And you could make it as an extension method:

public static class NumberExtensions
{
  public static ulong Factorial(this ulong number)
  {
    if (number &lt; 2) return 1;

    checked
    {
      ulong factorial = number;
      for (ulong i = 2; i &lt; number; i++)
      {
        factorial *= i;
      }
      return factorial;
    }
  }
}


Here I've widened the domain to ulong and I iterate upwards from 2 as it is more intuitive and easier to read. Further I've encapsulated the method in a checked statement in order to throw on overflow, because factorial grows quickly out of even the scope of ulong.



Usage

  while (true)
  {
    Console.Write($"Enter a number: ");
    if (ulong.TryParse(Console.ReadLine(), out ulong number))
    {
      try
      {
        Console.WriteLine($"Factorial of {number} = {number.Factorial()}");
      }
      catch (OverflowException ex)
      {
        Console.WriteLine($"Entered number is too big: {number}");
      }
      catch (Exception ex)
      {
        Console.WriteLine($"An Error occured: {ex.Message}");
      }
    }
    else
    {
      break;
    }
  }