The Program in C# Program to Rotate an array (length 3) of integers in left direction is given below:
using System;
namespace ArrayRotation
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family, Enter the integers below to rotate the array in the left direction: ");
Console.WriteLine("Well,enter the first integer: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Well,enter the second integer: ");
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Well,enter the third integer: ");
int num3 = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[3] { num1, num2, num3 };
int temp = arr[0];
for (int i = 0; i < arr.Length - 1; i++)
{
arr[i] = arr[i + 1];
}
arr[arr.Length - 1] = temp;
Console.WriteLine("Okay, after rotating the array in the left direction, the new array is: " + arr[0] + " " + arr[1] + " " + arr[2]);
Console.ReadLine();
}
}
}
Output:
Hello Codeauri Family, Enter the integers below to rotate the array in the left direction: Well,enter the first integer:
123
Well,enter the second integer:
456
Well,enter the third integer:
131
Okay, after rotating the array in the left direction, the new array is: 456 131 123
Pro-Tips💡
Here are the step by step execution of above program:
- Imports the “System” namespace.
- Defines a namespace called “ArrayRotation”.
- Defines a class called “Program” within the “ArrayRotation” namespace.
- Defines the “Main” method within the “Program” class.
- Writes a message to the console asking the user to enter three integers.
- Reads three integers from the console and stores them in variables “num1”, “num2”, and “num3”.
- Creates an array of integers called “arr” with the elements “num1”, “num2”, and “num3”.
- Stores the first element of the array in a temporary variable “temp”.
- Rotates the elements of the array to the left by shifting each element one position to the left and assigning it to the previous position.
- Assigns the value of the temporary variable “temp” to the last position of the array.
- Writes the new rotated array to the console.
- Reads a line from the console to wait for user input.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.