﻿ Java - Cyclically rotate a given array clockwise by one

# Java: Cyclically rotate a given array clockwise by one

## Java Array: Exercise-45 with Solution

Write a Java program to cyclically rotate a given array clockwise by one.

Pictorial Presentation: Sample Solution:

Java Code:

``````// Import the Arrays class to use the toString method for array display.
import java.util.Arrays;

// Define the Main class.
public class Main
{
// Define an integer array named 'arra'.
static int arra[] = new int[]{10, 20, 30, 40, 50, 60};

// Define a method to rotate the array elements.
static void rotate_array()
{
// Store the last element of the array in variable 'a'.
int a = arra[arra.length - 1];
int i;

// Iterate through the array to shift elements to the right.
for (i = arra.length - 1; i > 0; i--)
arra[i] = arra[i - 1];

// Set the first element of the array to 'a' (rotating it to the end).
arra = a;
}

// The main method for executing the program.
public static void main(String[] args)
{
// Display the original array.
System.out.println("Original array:");
System.out.println(Arrays.toString(arra));

// Call the rotate_array method to rotate the array.
rotate_array();

// Display the rotated array.
System.out.println("Rotated array:");
System.out.println(Arrays.toString(arra));
}
}
```
```

Sample Output:

```
Original arraay:
[10, 20, 30, 40, 50, 60]
Rotated arraay:
[60, 10, 20, 30, 40, 50]
```

Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿