Write a program to cyclically rotate an array by one in C#
- Get link
- X
- Other Apps
// C# code for program to cyclically// rotate an array by oneusing System;public class Test { static int[] arr = new int[] { 1, 2, 3, 4, 5 }; // Method for rotation static void rotate() { int last_el = arr[arr.Length - 1], i; for (i = arr.Length - 1; i > 0; i--) arr[i] = arr[i - 1]; arr[0] = last_el; } // Driver Code public static void Main() { Console.WriteLine("Given Array is"); string Original_array = string.Join(" ", arr); Console.WriteLine(Original_array); // Function Call rotate(); Console.WriteLine("Rotated Array is"); string Rotated_array = string.Join(" ", arr); Console.WriteLine(Rotated_array); }}// This code is contributed by vt_m. |
Output
Given array is 1 2 3 4 5 Rotated array is 5 1 2 3 4
- Get link
- X
- Other Apps
Comments
Post a Comment