C++ Examples

WAP in C++ to Rotate Elements of a given array of integers (length 4 ) in left direction and return the new array

The program in C++ to Rotate Elements of a given array of integers (length 4 ) in left direction and return the new array is given below:

#include <iostream>
using namespace std;
int main() {
    int arr[4];

    // Input the elements of the array
    cout << "Hello Codeauri Family,Enter the elements of the array to rotate the elements of a given array of integers here:\n ";
    for (int i = 0; i < 4; i++) {
        cin >> arr[i];
    }
    // Rotate the elements to the left
    int temp = arr[0];
    for (int i = 0; i < 3; i++) {
        arr[i] = arr[i+1];
    }
    arr[3] = temp;
    // Output the new array
    cout << "The new array is: ";
    for (int i = 0; i < 4; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}

Output:

Hello Codeauri Family,Enter the elements of the array to rotate the elements of a given array of integers here:
10
9
65
1
The new array is: 9 65 1 10

Pro-Tips💡

This program prompts the user to enter 4 integers as elements of an array.

It then uses a for loop to shift each element one position to the left, and assigns the first element to the last position.

Finally, it prints out the new array after rotation.

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

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…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

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