In this tutorial, we will learn java program to create an array and rotate the elements stored in the array by two positions.
That means if our array (list) is
![](https://quescol.com/wp-content/uploads/2022/06/right-rotation-in-array-java-program.jpg)
After two rotations
![](https://quescol.com/wp-content/uploads/2022/06/right-rotation-by-2-in-array-java-program.jpg)
Our program will first take the input of array size and then the elements of the array. Then, our program will rotate the elements of the array by 2
For example
Case 1: if the user inputs 4 as array (list) size and the array (list) elements as 1,2,3,4.
The output should be 3, 4, 1, 2.
Case 2: if the user inputs 5 as array (list) size and the array (list) elements as 9,8,7,6,5.
The output should be 6, 5, 9, 8,7.
Java Program for Right Rotation on Array Elements by Two Position
Output
![java program to perform right rotation by two on array](https://quescol.com/wp-content/uploads/2022/06/image-71.png)
Explanation
For the input array: [1, 2, 3, 4, 5], the for loop will rotate the array 2 times.
For 1st rotation the array becomes: [5, 1, 2, 3, 4].
And finally at 2nd rotation the array becomes: [4, 5, 1, 2, 3]