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.
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.
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#.
Summary:
I hope this post will be helpful to understand How to Perform Left Circular Rotation of an Array in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
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.
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:
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#.
Summary:
I hope this post will be helpful to understand How to Perform Left Circular Rotation of an Array in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.