Examples:
Input:
arr[ ]= {1,2,3,4,5,6}
Output:
arr[ ] = {6,1,2,3,4,5}
Input:
arr[ ]= {10,20,30,40,50,60}
Output:
arr[ ]= {60,10,20,30,40,50}
#include <iostream>
using namespace std;
// function to perform clockwise rotation
void rotate(int arr[], int n)
{
int temp = arr[n - 1], i;
for (i = n - 1; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = temp;
}
// Function to print the arr
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
// Main function
int main()
{
int arr[100], N, d;
cout << "Enter Number of elements: ";
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> arr[i];
}
rotate(arr, N);
cout << "Rotated array " << endl;
printArr(arr, N);
return 0;
}
Output:
Enter Number of elements: 5
1 2 3 4 5
Rotated array
5 1 2 3 4
0 Comments