C#  

Recursive Fibonacci Function in C#

Concept of Fibonacci Series

The Fibonacci series is a sequence of numbers where:

Each number is the sum of the two preceding numbers.

Mathematically

F(0) = 0  
F(1) = 1  
F(n) = F(n - 1) + F(n - 2)

Example Output
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

What is Recursion in Fibonacci?

Recursion means a function calls itself.
In this case, the Fibonacci function calls itself to calculate previous numbers.

Recursive Formula

Fibonacci(n) = Fibonacci(n - 1) + Fibonacci(n - 2)

Real-Time Example: Fibonacci in C# WebForms

Step 1: Design Page – RecursiveFibonacci.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RecursiveFibonacci.aspx.cs" Inherits="RecursiveFibonacci" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Recursive Fibonacci using C# WebForms</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f4f7fc;
            margin: 50px;
        }
        .container {
            width: 500px;
            margin: auto;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 0 10px #ccc;
            padding: 25px;
        }
        h2 {
            text-align: center;
            color: #1A2A80;
        }
        .form-control {
            width: 100%;
            padding: 8px;
            margin-top: 10px;
        }
        .btn {
            background-color: #7A85C1;
            color: white;
            border: none;
            padding: 10px;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
            margin-top: 10px;
        }
        .btn:hover {
            background-color: #5b68a1;
        }
        .result {
            margin-top: 20px;
            font-weight: bold;
            color: #333;
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h2>Recursive Fibonacci Function</h2>

            <asp:Label ID="lblInput" runat="server" Text="Enter Number of Terms:"></asp:Label><br />
            <asp:TextBox ID="txtNumber" runat="server" CssClass="form-control" placeholder="Example: 8"></asp:TextBox><br />

            <asp:Button ID="btnGenerate" runat="server" Text="Generate Fibonacci Series" CssClass="btn" OnClick="btnGenerate_Click" /><br />

            <asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
        </div>
    </form>
</body>
</html>

Step 2: Backend Logic – RecursiveFibonacci.aspx.cs

using System;
using System.Text;

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

    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        try
        {
            int terms = Convert.ToInt32(txtNumber.Text.Trim());

            if (terms <= 0)
            {
                lblResult.Text = "Please enter a positive number.";
                lblResult.ForeColor = System.Drawing.Color.Red;
                return;
            }

            StringBuilder series = new StringBuilder();

            for (int i = 0; i < terms; i++)
            {
                series.Append(Fibonacci(i) + " ");
            }

            lblResult.Text = "Fibonacci Series: " + series.ToString();
            lblResult.ForeColor = System.Drawing.Color.Green;
        }
        catch
        {
            lblResult.Text = "Invalid input! Please enter a valid number.";
            lblResult.ForeColor = System.Drawing.Color.Red;
        }
    }

    // Recursive function to find Fibonacci number
    private int Fibonacci(int n)
    {
        if (n == 0)
            return 0;  // Base case
        else if (n == 1)
            return 1;  // Base case
        else
            return Fibonacci(n - 1) + Fibonacci(n - 2); // Recursive case
    }
}

Output Example

InputOutput
5Fibonacci Series: 0 1 1 2 3
8Fibonacci Series: 0 1 1 2 3 5 8 13

Algorithm

Step 1: Start
Step 2: Read n (number of terms)
Step 3: For i = 0 to n-1
           Display Fibonacci(i)
Step 4: Function Fibonacci(n)
            If n == 0 return 0
            If n == 1 return 1
            Else return Fibonacci(n-1) + Fibonacci(n-2)
Step 5: Stop

Dry Run Example (n = 5)

CallReturns
Fibonacci(5)Fibonacci(4) + Fibonacci(3)
Fibonacci(4)Fibonacci(3) + Fibonacci(2)
Fibonacci(3)Fibonacci(2) + Fibonacci(1)
Fibonacci(2)Fibonacci(1) + Fibonacci(0)
Fibonacci(1)1
Fibonacci(0)0

Output Series: 0 1 1 2 3

Advantages

  • Simple and elegant approach.

  • Demonstrates clear understanding of recursion.

Disadvantages

  • Inefficient for large numbers (repeated calls).

  • Can be optimized using Memoization or Dynamic Programming.

🧾Conclusion

This example demonstrates how Recursion works beautifully in generating the Fibonacci sequence using C# WebForms.
It helps you understand the base case, recursive calls, and stack-based execution in real-world web applications.