- Java Online Training In Andhra Pradesh and Telangana

Opening Hours :7AM to 9PM

The above program creates an array arr of integers and assigns it the values {1, 2, 3, 4, 5}. It then defines a variable k that represents the number of elements by which the array should be rotated to the right. In this case, k is set to 2, so the array will be rotated by 2 elements to the right.class ArrayRightRotate { public void rightRotate(int[] arr, int k, int n) { for (int i = 0; i < k; i++) { int temp = arr[n - 1]; for (int j = n - 2; j >= 0; j--) { arr[j + 1] = arr[j]; } arr[0] = temp; } } } public class Main { public static void main(String[] args) { ArrayRightRotate ar=new ArrayRightRotate(); int[] arr = {1, 2, 3, 4, 5}; int n = arr.length; int k = 2; // rotate by 2 elements ar.rightRotate(arr, k, n); for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } }

The rightRotate method takes in 3 arguments; the array, how many elements to rotate, and the length of the array.

It then loops k times, where on each iteration the last element of the array is stored in a temporary variable, and then each element of the array is shifted one place to the right. At the end of each iteration, the first element of the array is assigned the value of the temporary variable.

You can rotate the array multiple times by increasing the value of k or by running the loop again.

- 13 . Sunny Number in Java

- 14 . Tech Number in Java

- 15 . Keith Number in Java

- 16 . Neon Number in Java

- 17 . Spy Number in Java

- 18 . ATM program Java

- 19 . Emirp Number in Java

- 54 . Write a Java program to count the letters, spaces, numbers and other characters of an input string

- 100 . Write a Java program that takes a year from user and print whether that year is a leap year or not

- Duaration(Core & Advanced) : 90 Hours
- Leactures : 90
- Quizzes : 60
- Interview Questions : 600
- Important Programs: 300
- Seminars: 20