Write C# program to left rotate an array

Introduction

Write C# program to left rotate an array: I have used Visual Studio 2012 for debugging purpose. But you can use any version of visual studio as per your availability.

using System;
class Program
{   
    static void Main()
    {
        int[] numbers = { 1, 2, 8 };
        Console.WriteLine("\nArray1: [{0}]", string.Join(", ", numbers));
        var temp = numbers[0];
        for (var i = 0; i < numbers.Length - 1; i++)
        {
            numbers[i] = numbers[i + 1];
        }
        numbers[numbers.Length - 1] = temp;
        Console.WriteLine("\nAfter rotating array becomes: [{0}]", string.Join(", ", numbers));
 
 
        Console.ReadLine();
    }
 
}

Result

Write C# program to left rotate an array
Write C# program to left rotate an array

 

Leave a Comment

x