C#  

To rotate array elements left/right

Step 1: Design Page – RotateArray.aspx

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Rotate Array Elements - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f4f7fc;
            margin: 50px;
        }
        .container {
            width: 550px;
            margin: auto;
            background: #fff;
            border-radius: 10px;
            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;
            border-radius: 5px;
            cursor: pointer;
            margin-top: 10px;
            width: 48%;
        }
        .btn:hover {
            background-color: #5d66a1;
        }
        .result {
            margin-top: 20px;
            font-weight: bold;
            color: #333;
            text-align: center;
            white-space: pre-line;
        }
        .button-group {
            display: flex;
            justify-content: space-between;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h2>Rotate Array Elements (Left / Right)</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, 20, 30, 40, 50"></asp:TextBox><br />

            <asp:Label ID="lblRotate" runat="server" Text="Enter Number of Rotations:"></asp:Label><br />
            <asp:TextBox ID="txtRotateCount" runat="server" CssClass="form-control" placeholder="Example: 2"></asp:TextBox><br />

            <div class="button-group">
                <asp:Button ID="btnLeft" runat="server" Text="Rotate Left" CssClass="btn" OnClick="btnLeft_Click" />
                <asp:Button ID="btnRight" runat="server" Text="Rotate Right" CssClass="btn" OnClick="btnRight_Click" />
            </div>

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

Step 2: Backend Logic – RotateArray.aspx.cs

using System;
using System.Linq;

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

    // Rotate Left Button Click
    protected void btnLeft_Click(object sender, EventArgs e)
    {
        RotateArrayElements("Left");
    }

    // Rotate Right Button Click
    protected void btnRight_Click(object sender, EventArgs e)
    {
        RotateArrayElements("Right");
    }

    private void RotateArrayElements(string direction)
    {
        try
        {
            string input = txtNumbers.Text.Trim();
            int rotateCount = Convert.ToInt32(txtRotateCount.Text.Trim());

            if (string.IsNullOrEmpty(input))
            {
                lblResult.Text = "Please enter array elements.";
                lblResult.ForeColor = System.Drawing.Color.Red;
                return;
            }

            int[] numbers = input.Split(',').Select(x => Convert.ToInt32(x.Trim())).ToArray();
            int n = numbers.Length;

            if (rotateCount < 0)
            {
                lblResult.Text = "Rotation count cannot be negative.";
                lblResult.ForeColor = System.Drawing.Color.Red;
                return;
            }

            rotateCount = rotateCount % n; // handle large rotations

            int[] rotated = new int[n];

            if (direction == "Left")
            {
                for (int i = 0; i < n; i++)
                {
                    rotated[i] = numbers[(i + rotateCount) % n];
                }
            }
            else // Right rotation
            {
                for (int i = 0; i < n; i++)
                {
                    rotated[(i + rotateCount) % n] = numbers[i];
                }
            }

            lblResult.Text = $"Original Array: {string.Join(", ", numbers)}\n" +
                             $"{direction} Rotated by {rotateCount}: {string.Join(", ", rotated)}";
            lblResult.ForeColor = System.Drawing.Color.Green;
        }
        catch
        {
            lblResult.Text = "Invalid input! Please enter valid numbers and rotation count.";
            lblResult.ForeColor = System.Drawing.Color.Red;
        }
    }
}

Real-Time Example Flow

  1. Open the page RotateArray.aspx in your browser.

  2. Enter:

    10, 20, 30, 40, 50
    

    and rotation count:

    2
    
  3. Click Rotate Left
    Output

    Original Array: 10, 20, 30, 40, 50
    Left Rotated by 2: 30, 40, 50, 10, 20
    
  4. Click Rotate Right
    Output

    Original Array: 10, 20, 30, 40, 50
    Right Rotated by 2: 40, 50, 10, 20, 30
    

Explanation

StepDescription
1User enters comma-separated numbers and number of rotations.
2Program converts them into an integer array.
3For left rotation: shift each element left by rotateCount.
4For right rotation: shift each element right by rotateCount.
5Display original and rotated arrays on the web page.

Example Input / Output Table

Input ArrayRotationsDirectionOutput
10, 20, 30, 40, 502Left30, 40, 50, 10, 20
10, 20, 30, 40, 501Right50, 10, 20, 30, 40
5, 10, 15, 203Left20, 5, 10, 15
100, 200, 3004Right200, 300, 100

Algorithm

1. Read array elements and rotation count.
2. If direction = Left:
      rotated[i] = arr[(i + count) % n]
   Else if direction = Right:
      rotated[(i + count) % n] = arr[i]
3. Print rotated array.