## Q. Write a program for array rotation.

Array Rotation:- Array rotation means moving array elements to the left or right of an array by specific position. An array can be moved to the number of given positions by rotating left (clockwise) or right (anti-clockwise). Here we will be discussing left array rotation.

# Program For Array Rotation

• C
• C++
• Java
• Python
• C#
• PHP
• ```// Program For Array Rotation In C
#include <stdio.h>
void left_rotation_by_one(int rotate[], int len);
void left_rotate(int rotate[], int times, int len)
{
int i;
for (i = 0; i < times; i++)
left_rotation_by_one(rotate, len);
}
void left_rotation_by_one(int rotate[], int len)
{
int temp = rotate[0], i;
for (i = 0; i < len - 1; i++)
rotate[i] = rotate[i + 1];
rotate[i] = temp;
}
void print_array(int rotate[], int len)
{
int i;
for (i = 0; i < len; i++)
printf("%d ", rotate[i]);
}
int main()
{
int rotate[] = { 10, 20, 30, 40, 50, 60, 70 };
left_rotate(rotate, 3, 7);
print_array(rotate, 7);
return 0;
} ```
```// Program For Array Rotation In C++
#include <iostream>
using namespace std;
void left_rotation_by_one(int rotate[], int len)
{
int temp = rotate[0], i;
for (i = 0; i < len - 1; i++)
rotate[i] = rotate[i + 1];
rotate[i] = temp;
}
void left_rotate(int rotate[], int times, int len)
{
for (int i = 0; i < times; i++)
left_rotation_by_one(rotate, len);
}
void print_array(int rotate[], int len)
{
for (int i = 0; i < len; i++)
cout << rotate[i] << " ";
}
int main()
{
int rotate[] = { 10, 20, 30, 40, 50, 60, 70 };
int len = sizeof(rotate) / sizeof(rotate[0]);
left_rotate(rotate, 3, len);
print_array(rotate, len);
return 0;
}
```
```// Program For Array Rotation In Java
class LFC {
void left_rotate(int rotate[], int times, int len)
{
for (int i = 0; i < times; i++)
left_rotate_by_one(rotate, len);
}
void left_rotate_by_one(int rotate[], int len)
{
int i, temp;
temp = rotate[0];
for (i = 0; i < len - 1; i++)
rotate[i] = rotate[i + 1];
rotate[i] = temp;
}
void print_array(int rotate[], int len)
{
for (int i = 0; i < len; i++)
System.out.print(rotate[i] + " ");
}
public static void main(String[] args)
{
LFC rot = new LFC();
int rotate[] = { 10, 20, 30, 40, 50, 60, 70 };
rot.left_rotate(rotate, 3, 7);
rot.print_array(rotate, 7);
}
} ```
```// Program For Array Rotation In Python
def left_rotate(rotate, times, length):
for i in range(times):
left_rotate_by_ne(rotate, length)
def left_rotate_by_ne(rotate, length):
temp = rotate[0]
for i in range(length-1):
rotate[i] = rotate[i + 1]
rotate[length-1] = temp
def print_array(rotate, length):
for i in range(length):
print ("% d"% rotate[i], end =" ")
rotate = [10, 20, 30, 40, 50, 60, 70]
left_rotate(rotate, 3, 7)
print_array(rotate, 7)
```
```// Program For Array Rotation In C#
using System;
class LFC {
static void left_rotate(int[] rotate, int times,
int len)
{
for (int i = 0; i < times; i++)
left_rotate_by_one(rotate, len);
}
static void left_rotate_by_one(int[] rotate, int len)
{
int i, temp = rotate[0];
for (i = 0; i < len - 1; i++)
rotate[i] = rotate[i + 1];
rotate[i] = temp;
}
static void print_array(int[] rotate, int len)
{
for (int i = 0; i < len; i++)
Console.Write(rotate[i] + " ");
}
public static void Main()
{
int[] rotate = { 10, 20, 30, 40, 50, 60, 70 };
left_rotate(rotate, 3, 7);
print_array(rotate, 7);
}
}
```
```// Program For Array Rotation In PHP
function left_rotate_by_one(&\$rotate, \$len)
{
\$temp = \$rotate[0];
for (\$i = 0; \$i < \$len - 1; \$i++)
\$rotate[\$i] = \$rotate[\$i + 1];
\$rotate[\$i] = \$temp;
}
function left_rotate(&\$rotate, \$times, \$len)
{
for (\$i = 0; \$i < \$times; \$i++)
left_rotate_by_one(\$rotate, \$len);
}
function print_array(&\$rotate, \$len)
{
for (\$i = 0; \$i < \$len; \$i++)
echo \$rotate[\$i] . " ";
}
\$rotate = array( 10, 20, 30, 40, 50, 60, 70 );
\$len = sizeof(\$rotate);
left_rotate(\$rotate, 3, \$len);
print_array(\$rotate, \$len);
```
```// Program For Array Rotation In C
#include <stdio.h>
void left_rotation_by_one(int rotate[], int len);
void left_rotate(int rotate[], int times, int len)
{
int i;
for (i = 0; i < times; i++)
left_rotation_by_one(rotate, len);
}
void left_rotation_by_one(int rotate[], int len)
{
int temp = rotate[0], i;
for (i = 0; i < len - 1; i++)
rotate[i] = rotate[i + 1];
rotate[i] = temp;
}
void print_array(int rotate[], int len)
{
int i;
for (i = 0; i < len; i++)
printf("%d ", rotate[i]);
}
int main()
{
int rotate[] = { 10, 20, 30, 40, 50, 60, 70 };
left_rotate(rotate, 3, 7);
print_array(rotate, 7);
return 0;
}
```

#### Output

`40, 50, 60, 70, 10, 20, 30`

## Recommended Programs

Bubble sort program (Descending Order)
Bubble sort program (Ascending Order)