GeeksforGeeks App
Open App
Browser
Continue

# C Program for Program to cyclically rotate an array by one

Given an array, cyclically rotate the array clockwise by one.

Examples:

```Input  :  arr[] = {1, 2, 3, 4, 5}
Output : arr[] = {5, 1, 2, 3, 4}```

## C++

 `// C++ code for program to``// cyclically rotate an array by one` `# include ` `using` `namespace` `std;` `// Method``void` `rotate(``int` `arr[], ``int` `n)``{``    ``int` `x = arr[n - 1], i;``    ``for` `(i = n - 1; i > 0; i--)``    ``arr[i] = arr[i - 1];``    ``arr[0] = x;``}` `// Main driver method``int` `main()``{``    ``int` `arr[] = {1, 2, 3, 4, 5}, i;``    ``int` `n = ``sizeof``(arr) /``            ``sizeof``(arr[0]);` `    ``cout << ``"Given array is \n"``;``    ``for` `(i = 0; i < n; i++)``        ``cout << arr[i];` `    ``rotate(arr, n);` `    ``cout << ``"\nRotated array is\n"``;``    ``for` `(i = 0; i < n; i++)``        ``cout << arr[i];` `    ``return` `0;``}` `// This code is contributed by jit_t`

## C

 `#include ` `void` `rotate(``int` `arr[], ``int` `n)``{``   ``int` `x = arr[n-1], i;``   ``for` `(i = n-1; i > 0; i--)``      ``arr[i] = arr[i-1];``   ``arr[0] = x;``}` `int` `main()``{``    ``int` `arr[] = {1, 2, 3, 4, 5}, i;``    ``int` `n = ``sizeof``(arr)/``sizeof``(arr[0]);` `    ``printf``(``"Given array is\n"``);``    ``for` `(i = 0; i < n; i++)``        ``printf``(``"%d "``, arr[i]);` `    ``rotate(arr, n);` `    ``printf``(``"\nRotated array is\n"``);``    ``for` `(i = 0; i < n; i++)``        ``printf``(``"%d "``, arr[i]);` `    ``return` `0;``}`

Output

```Given array is
12345
Rotated array is
51234```

Time Complexity: O(n)

Auxiliary Space: O(1)

Please refer complete article on Program to cyclically rotate an array by one for more details!

My Personal Notes arrow_drop_up