Medium csharp

Factorial of a number

Problem

Compute factorial of 5 using a loop and print the result (120).

Hints
  • Initialize fact = 1 and multiply in a for loop from 1 to n.

Your practice code

Ready — edit the code above and click Run.

Solution

using System;

class Program
{
    static void Main()
    {
        int n = 5;
        long fact = 1;
        for (int i = 1; i <= n; i++)
            fact *= i;
        Console.WriteLine(fact);
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Iterative factorial avoids stack overflow for larger n. Use long for bigger values.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details