Basic Programming C# Examples

C# Program to Rotate an array (length 3) of integers in left direction

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:

  1. Imports the “System” namespace.
  2. Defines a namespace called “ArrayRotation”.
  3. Defines a class called “Program” within the “ArrayRotation” namespace.
  4. Defines the “Main” method within the “Program” class.
  5. Writes a message to the console asking the user to enter three integers.
  6. Reads three integers from the console and stores them in variables “num1”, “num2”, and “num3”.
  7. Creates an array of integers called “arr” with the elements “num1”, “num2”, and “num3”.
  8. Stores the first element of the array in a temporary variable “temp”.
  9. 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.
  10. Assigns the value of the temporary variable “temp” to the last position of the array.
  11. Writes the new rotated array to the console.
  12. 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X