Right Rotation of Array in C Langauge – Right Rotate Array N Times


Program Description:

Write a Program to understand the Right Rotation of Array in C Programming Language. The user will input an array and the number of positions to be rotated to the right(Right Rotation). Then the program will rotate the specified number of elements in the array in the right direction.

Here is the example input and output of the program

Example Input and Output:


Enter desired size for array(1-100): 6

Please enter array elements(6) : 34 54 78 28 12 99

Enter the number of position to rotate the array: 2


Original Array: 34 54 78 28 12 99

Array After Right Rotating 2 Positions : 78 28 12 99 34 54


As we can see from the above output When the user provided the input array as 34 54 78 28 12 99 and the number of positions as 2, Then the program right rotated the array by 2 elements and displayed the resultant array 78 28 12 99 34 54 on console.

📢This Program is part of the Array Practice Programs Series


It is recommended to go through the following articles to learn more about the C Array and C Functions.

  1. Introduction to C Arrays – How to Declare, Access, and Modify Arrays
  2. How to pass arrays to functions in C
  3. Functions in C Language

Algorithm for Right Rotation of Array in C:

Let’s look at the Algorithm for Right Rotating an Array in the C language

Let’s say the input array is numbers array and the total number of the array elements are equal to size. and The variable positioncontains the number of elements that need to be rotated in the Right side direction.

  • Array is numbers
  • The size of the array is size
  • The number of positions to be rotated is position

To Rotate the array by positionelements,

  • Create a Temporary Array( tempArr) with the same size as the input array numbers
  • Now, We need to iterate over the input array ( numbers) and insert the elements in the temporary array( tempArr) at the correct index after rotation of the position number of elements.
  • The correct position after rotating the array by position position can be calculated using the formula – (i + size - position) % size.
    • Where, i is the present element index ( for loop index)
    • position is the number of positions to be shifted in the Right direction
    • size is the total size of the input array.
  • Once the above loop is completed, The temporary array ( tempArr) contains the Array which is rotated by position number of elements.
  • Replace the Input array ( numbers) elements with the Temporary array ( tempArr) elements.
  • Now, The numbers array contains the Right Rotated array.

Now, Let’s look at the step-by-step explanation of the Array Right Rotation Program in C language

Right Rotation of Array in C Program Explanation:

  1. Start the program by declaring two arrays. Input Array which is numbers and Temporary Array which is tempArr. The max size of both arrays is equal to 100. ( Change the 100 max size, if you want to use large arrays – Use can also use a macro or const int to hold the 100 value, For brevity we are directly using the number).
  2. Take the desired array size from the user and store it in the size variable.
  3. Check if the size is going beyond the array bounds using – (size >= 100 || size <= 0) condition. Display an error message if the array is going out of bounds.
  4. We define two helper functions named read() function and display() function to read and print array elements
    • The read() function is used to read the input elements from the user and update the numbers array
    • The display() function is used to print the array elements on the Standard Output
  5. Now Call the read() function with numbers array and size as the parameter to Take user input and update the numbers array. – read(numbers, size);
  6. Make another function call to display() function to print the array elements on the console. – display(numbers, size);
  7. To Right Rotate the array of elements by position number of elements,
    • Iterate over the original array from i=0 to i<size using a For Loop
    • Copy the numbers[i] to tempArr array at ((i+size-position) % size) index
    • tempArr[((i+size-position) % size)] = numbers[i];
    • Increment the i by 1.
  8. Once the above loop is completed, The tempArr contains the Right Rotated Array.
  9. Replace the original array elements with the tempArr elements.
    • Use another For loop start from i=0 to i<size
    • Copy tempArr[i] to the numbers[i]numbers[i] = tempArr[i];
  10. Display the Right rotated array on the console by calling the display() function from the main() function. – display(numbers, size);
  11. Stop the program.

Program to Understand the Right Rotation of Array in C Language:

Convert the above algorithm into the C Program. Here is the Program to Right Rotate the array elements by ‘position’ positions in C language.

Program Output:

Compile and Run the program using GCC compiler (Any Compiler)

$ gcc right-rotate-arr.c

Run the executable file.

Test 1: Test the normal cases:

Let’s try a few more examples


As we can see from the above outputs, The program is properly Right Rotating the elements.

For example, When the user provided the 10 20 30 40 array as input and 3as the positions to rotate, The program right-rotated the array and provided the 40 10 20 30 array as the output.

Test 2: When the user enters invalid Sizes

As excepted, when the user enters an invalid size, The program is displaying an error message ( Invalid Size, Try again).

Related Array Programs:


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply