C#  

Find the Largest and Smallest number in an array

Step 1: Design Page – LargestSmallest.aspx

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Find Largest and Smallest Number in Array - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f0f2f5;
            margin: 50px;
        }
        .container {
            width: 450px;
            margin: auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0px 0px 10px #ccc;
            padding: 20px;
        }
        h2 {
            color: #1A2A80;
            text-align: center;
        }
        .form-control {
            width: 100%;
            padding: 8px;
            margin-top: 10px;
        }
        .btn {
            background-color: #7A85C1;
            color: white;
            border: none;
            padding: 10px 20px;
            margin-top: 10px;
            border-radius: 5px;
            cursor: pointer;
        }
        .result {
            font-weight: bold;
            color: #333;
            margin-top: 15px;
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h2>Find Largest and Smallest in Array</h2>
            
            <asp:Label ID="lblArray" runat="server" Text="Enter Numbers (comma separated):"></asp:Label><br />
            <asp:TextBox ID="txtArray" runat="server" CssClass="form-control" 
                placeholder="Example: 10, 25, 5, 42, 7"></asp:TextBox><br />
            
            <asp:Button ID="btnFind" runat="server" Text="Find Largest & Smallest" 
                CssClass="btn" OnClick="btnFind_Click" /><br />
            
            <asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
        </div>
    </form>
</body>
</html>

Step 2: Backend Logic – LargestSmallest.aspx.cs

using System;
using System.Linq;

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

    protected void btnFind_Click(object sender, EventArgs e)
    {
        try
        {
            // Get input numbers from textbox
            string[] input = txtArray.Text.Trim().Split(',');
            int[] numbers = input.Select(x => int.Parse(x.Trim())).ToArray();

            // Find largest and smallest numbers
            int largest = numbers.Max();
            int smallest = numbers.Min();

            lblResult.Text = $"Largest Number: {largest}<br/>Smallest Number: {smallest}";
            lblResult.ForeColor = System.Drawing.Color.Green;
        }
        catch
        {
            lblResult.Text = "Please enter valid comma-separated numbers.";
            lblResult.ForeColor = System.Drawing.Color.Red;
        }
    }
}

Real-Time Example Flow

  1. Run the page → LargestSmallest.aspx

  2. Enter a list of numbers like:

    12, 5, 78, 3, 45, 90, 10
    
  3. Click “Find Largest & Smallest”

  4. The result displays:

    Largest Number: 90

    Smallest Number: 3

Explanation

ComponentPurpose
TextBox (txtArray)User enters numbers separated by commas.
Button (btnFind)Executes backend logic when clicked.
LINQ (Max, Min)Efficiently finds the largest and smallest values.
Label (lblResult)Displays the final output dynamically.

Sample Input / Output

InputLargestSmallest
10, 25, 5, 42, 7425
100, 250, 999, 10, 3399910
3, 1, 4, 241