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


Introduction

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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Arrays;
  2. public class Javaexcercise
  3. {
  4. static int arra[] = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
  5.  
  6. static void rotate_array()
  7. {
  8. int num = arra[arra.length-1], i;
  9. for (i = arra.length-1; i > 0; i--)
  10. arra[i] = arra[i-1];
  11. arra[0] = num;
  12. }
  13.  
  14. public static void main(String[] args)
  15. {
  16. System.out.println("Original arraay:");
  17. System.out.println(Arrays.toString(arra));
  18.  
  19. rotate_array();
  20.  
  21. System.out.println("Rotated arraay:");
  22. System.out.println(Arrays.toString(arra));
  23. }
  24. }

Result

java techstudy.org

     





Related Topics




Popular Posts