C#  

Count even and odd elements in an array

Step 1: Design Page – CountEvenOddArray.aspx

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Count Even and Odd Numbers in Array - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f4f7fc;
            margin: 50px;
        }
        .container {
            width: 500px;
            margin: auto;
            background: #fff;
            border-radius: 8px;
            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;
            margin-top: 10px;
            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>Count Even and Odd Elements</h2>

            <asp:Label ID="lblInput" runat="server" Text="Enter Array Elements (comma-separated):"></asp:Label><br />
            <asp:TextBox ID="txtNumbers" runat="server" CssClass="form-control" placeholder="Example: 10, 15, 22, 33, 40, 55"></asp:TextBox><br />

            <asp:Button ID="btnCountEvenOdd" runat="server" Text="Count Even & Odd" CssClass="btn" OnClick="btnCountEvenOdd_Click" /><br />

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

Step 2: Backend Logic – CountEvenOddArray.aspx.cs

using System;
using System.Linq;

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

    protected void btnCountEvenOdd_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
        {
            // Convert comma-separated input into an integer array
            int[] numbers = input.Split(',').Select(x => Convert.ToInt32(x.Trim())).ToArray();

            int evenCount = 0;
            int oddCount = 0;

            // Loop through the array and count even/odd
            foreach (int num in numbers)
            {
                if (num % 2 == 0)
                    evenCount++;
                else
                    oddCount++;
            }

            lblResult.Text = $"Array Elements: {string.Join(", ", numbers)}\n\n" +
                             $"Even Numbers: {evenCount}\n" +
                             $"Odd Numbers: {oddCount}";
            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. Run the project and open CountEvenOddArray.aspx.

  2. Enter the array, for example:

    10, 15, 22, 33, 40, 55
    
  3. Click “Count Even & Odd”.

  4. Output:

    Array Elements: 10, 15, 22, 33, 40, 55
    
    Even Numbers: 3
    Odd Numbers: 3
    

Explanation

StepDescription
1Takes user input as comma-separated values.
2Converts input into an integer array using Split() and Select().
3Uses a loop to count even and odd numbers.
4Displays both counts dynamically on the web page.

Example Input / Output Table

InputOutput
10, 20, 30, 40Even: 4, Odd: 0
5, 7, 9Even: 0, Odd: 3
11, 22, 33, 44, 55Even: 2, Odd: 3
1, 2, 3, 4, 5, 6Even: 3, Odd: 3

Algorithm

1. Read array elements from user.
2. Initialize evenCount = 0, oddCount = 0.
3. For each element in the array:
      If element % 2 == 0 → evenCount++
      Else → oddCount++
4. Display evenCount and oddCount.