C#  

Find sum and average of array elements

Step 1: Design Page – SumAndAverage.aspx

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Sum and Average of Array Elements - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f0f2f5;
            margin: 50px;
        }
        .container {
            width: 520px;
            margin: auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0px 0px 10px #ccc;
            padding: 25px;
        }
        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;
            margin-top: 10px;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
        }
        .result {
            margin-top: 20px;
            font-weight: bold;
            color: #333;
            text-align: center;
            white-space: pre-line;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h2>Find Sum & Average of Array Elements</h2>

            <asp:Label ID="lblInput" runat="server" Text="Enter Numbers (comma-separated):"></asp:Label><br />
            <asp:TextBox ID="txtNumbers" runat="server" CssClass="form-control" placeholder="Example: 10,20,30,40,50"></asp:TextBox><br />

            <asp:Button ID="btnCalculate" runat="server" Text="Calculate Sum & Average" CssClass="btn" OnClick="btnCalculate_Click" /><br />

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

Step 2: Backend Logic – SumAndAverage.aspx.cs

using System;
using System.Linq;

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

    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        string input = txtNumbers.Text.Trim();

        if (string.IsNullOrEmpty(input))
        {
            lblResult.Text = "Please enter numbers separated by commas.";
            lblResult.ForeColor = System.Drawing.Color.Red;
            return;
        }

        try
        {
            // Split input into array and convert to integers
            int[] numbers = input.Split(',').Select(n => Convert.ToInt32(n.Trim())).ToArray();

            int sum = numbers.Sum();
            double average = numbers.Average();

            lblResult.Text = $"Array Elements: {string.Join(", ", numbers)}\n\n" +
                             $"Sum of Elements: {sum}\n" +
                             $"Average of Elements: {average:F2}";
            lblResult.ForeColor = System.Drawing.Color.Green;
        }
        catch
        {
            lblResult.Text = "Invalid input! Please enter only numbers separated by commas.";
            lblResult.ForeColor = System.Drawing.Color.Red;
        }
    }
}

Real-Time Example Flow

  1. Open SumAndAverage.aspx in your browser.

  2. Enter:
    10, 20, 30, 40, 50

  3. Click “Calculate Sum & Average”

  4. Output

    Array Elements: 10, 20, 30, 40, 50
    
    Sum of Elements: 150
    Average of Elements: 30.00
    

Explanation

StepDescription
1Accept comma-separated numbers from the user.
2Convert the string into an integer array using Split() and Select().
3Use LINQ functions Sum() and Average() to calculate results.
4Display both values in a clear formatted output.

Example Input / Output Table

InputOutput (Sum / Average)
10, 20, 30, 40, 50Sum = 150, Average = 30.00
5, 15, 25Sum = 45, Average = 15.00
100, 200, 300, 400Sum = 1000, Average = 250.00
7, 14, 21, 28, 35Sum = 105, Average = 21.00