File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package easyq ;
2+ /* BY-M.NAVEEN
3+ * engineerscodes
4+ * code -Program for array rotation
5+ * sr.code-Ar1
6+ * */
7+ import java .util .*;
8+ public class cyclically_rotate extends Object //class name
9+ { static Scanner nav =new Scanner (System .in );
10+ static int array [],size ; //Declaring Variables and array
11+ int y ;
12+ public cyclically_rotate (int c )
13+ {
14+ size =c ;
15+ array =new int [size ]; //initialization of size and array
16+ }
17+ public static void rotate () //method to roatate the array n times
18+ {
19+ int temp =array [0 ];
20+ for (int i =1 ;i <size ;i ++)
21+ array [i -1 ]=array [i ];
22+ array [size -1 ]=temp ;
23+
24+ System .out .println ("element in array are::" +Arrays .toString (array )); //statement to print rotated array
25+ }
26+
27+
28+ public static void input () //input method to take input elements in array
29+ {
30+ for (int i =0 ;i <array .length ;i ++)
31+ array [i ]=nav .nextInt ();
32+ System .out .println ("element in array are::" +Arrays .toString (array )); //To print the elements in array
33+ }
34+
35+ public static void main (String arg []) //main function to call member fuction of class
36+ {
37+ System .out .println ("enter the size of the array" );
38+ cyclically_rotate n =new cyclically_rotate (nav .nextInt ()); //calling constructor of class cyclically_rotate
39+ System .out .println ("Enter the element of array" );
40+ input (); //calling input method
41+ while (true ) //while loop to call rotate method n times at runtime
42+ {
43+ System .out .println ("enter 1)shift or 2)exit" );
44+ switch (nav .nextInt ())
45+ {
46+ case 1 :rotate (); //calling rotate method
47+ break ;
48+ case 2 : System .exit (0 ); //To exit the program
49+ }
50+
51+ }
52+ }
53+ }
54+
You can’t perform that action at this time.
0 commit comments