How to Perform Left Circular Rotation of an Array in C#

How to Perform Left Circular Rotation of an Array in C#

In this article, I am going to discuss How to Perform Left Circular Rotation of an Array in C# with an example. Please read our previous article where we discussed how to convert a one-dimensional array to a two-dimensional array in C# with an example.

Program Description:

Here, the user will input an integer array. Then we need to shift each element of the input array to its left by one position in the circular fashion. The logic we need to implement should iterate the loop from Length – 1 to 0 and then swap each element with the last element. Please have a look at the following diagram for a better understanding of what we want to achieve.

How to Perform Left Circular Rotation of an Array in C#

Left Circular Rotation of an Array in C#:

In the following example, first we take the input integer array from the user. Then we perform the left circular rotation using for loop.

using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] oneDimensionalArray = new int[6];
            Console.WriteLine("Enter the 1D Array Elements : ");
            for (int i = 0; i<oneDimensionalArray.Length; i++)
            {
                oneDimensionalArray[i] = int.Parse(Console.ReadLine());
            }
            
            int temp;
            for (int j = 0; j < oneDimensionalArray.Length - 1; j++)
            {
                temp = oneDimensionalArray[0];
                oneDimensionalArray[0] = oneDimensionalArray[j + 1];
                oneDimensionalArray[j + 1] = temp;
            }

            Console.WriteLine("Array Elements After Right Circular Rotation: ");
            foreach (int num in oneDimensionalArray)
            {
                Console.Write(num + " ");
            }
            
            Console.ReadKey();
        }
    }
}

Output:

How to Perform Left Circular Rotation of an Array in C#

In the next article, I am going to discuss how to perform the Right circular rotation of an array in C# with examples. I hope now you understood How to Perform Left Circular Rotation of an Array in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *