Q:

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

belongs to collection: Array Programs in Java with Examples

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Arrays;
 public class Javaexcercise
{
    static int arra[] = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
 
    static void rotate_array()
    {
       int num = arra[arra.length-1], i;
       for (i = arra.length-1; i > 0; i--)
          arra[i] = arra[i-1];
       arra[0] = num;
    }
 
   public static void main(String[] args) 
    {
        System.out.println("Original arraay:");
        System.out.println(Arrays.toString(arra));
 
        rotate_array();
 
        System.out.println("Rotated arraay:");
        System.out.println(Arrays.toString(arra));
    }
}

Result:

Original arraay:
[1, 2, 3, 4, 5, 6, 7, 8]
Rotated arraay:
[8, 1, 2, 3, 4, 5, 6, 7]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Array Programs in Java with Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to print all unique element i... >>
<< Write a Java program to segregate all 0s on left s...