ASP.NET Core  

Factorial Calculator (ASP.NET WebForms Example)

Let’s create a real-time web form example in ASP.NET (C#) to find the Factorial of a number.

This example will include:

  • A TextBox for user input

  • A Button to calculate

  • A Label to show the factorial result

  • C# backend logic to handle the factorial calculation

Step 1: Design (Default.aspx)

Here’s your HTML WebForm design code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FactorialWebApp.Default" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Factorial Calculator</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f8f8f8;
            text-align: center;
            padding: 50px;
        }
        .card {
            background-color: white;
            border-radius: 10px;
            padding: 30px;
            width: 400px;
            margin: auto;
            box-shadow: 0px 0px 10px #ccc;
        }
        h2 { color: #1A2A80; }
        input, button {
            margin: 10px;
            padding: 8px;
            font-size: 16px;
        }
        .result {
            font-weight: bold;
            color: green;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="card">
            <h2>Factorial Calculator</h2>
            <asp:Label ID="Label1" runat="server" Text="Enter a number: "></asp:Label>
            <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="btnCalculate" runat="server" Text="Calculate Factorial" OnClick="btnCalculate_Click" />
            <br /><br />
            <asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
        </div>
    </form>
</body>
</html>

Step 2: Backend Logic (Default.aspx.cs)

Now add the C# code behind the page:

using System;

namespace FactorialWebApp
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                int number = int.Parse(txtNumber.Text);
                long result = CalculateFactorial(number);
                lblResult.Text = $"Factorial of {number} is: {result}";
            }
            catch (FormatException)
            {
                lblResult.Text = " Please enter a valid integer number.";
            }
            catch (Exception ex)
            {
                lblResult.Text = $"Error: {ex.Message}";
            }
        }

        private long CalculateFactorial(int n)
        {
            if (n < 0)
                throw new Exception("Number must be non-negative.");

            long fact = 1;
            for (int i = 1; i <= n; i++)
            {
                fact *= i;
            }
            return fact;
        }
    }
}

How It Works (Real-Time Flow)

  1. User enters a number in the TextBox.

  2. On clicking “Calculate Factorial”, the button triggers btnCalculate_Click.

  3. The backend reads the input and calls the CalculateFactorial() method.

  4. The factorial result is calculated and displayed in the Label.

  5. If user enters invalid input (like letters or negative numbers), an error message appears.

Real-World Use Case Example

Suppose you are creating a Math Learning Web App or Online Exam Portal,
and one of the modules allows students to practice math problems like factorial, prime, Fibonacci, etc.
This same approach is used for real-time quiz or learning applications.

Output Example

If the user enters 5,
the result shown will be:

Factorial of 5 is: 120