## 13. C program to right rotate the elements of an array

In this program, we need to rotate the elements of array towards its right by the specified number of times. An array is said to be right rotated if all elements of the array are moved to its right by one position. One approach is to loop through the array by shifting each element of the array to its next position. The last element of the array will become the first element of the rotated array.

Consider above array, if n is 1 then, all elements of the array will be moved to its right by one position that is the first element of the array will take the second position, the second element will be moved to the third position and so on. The last element of the array will become the first element of the array.

### ALGORITHM:

• STEP 1: START
• STEP 2: INITIALIZE arr[] ={1, 2, 3, 4, 5 }.
• STEP 3: length= sizeof(arr)/sizeof(arr[0])
• STEP 4: SET n =3
• STEP 5: PRINT "Original Array"
• STEP 6: SET i=0. REPEAT STEP 7 and STEP 8 UNTIL i<length
• STEP 7: PRINT arr[i]
• STEP 8: i=i+1.
• STEP 9: SET i=0. REPEAT STEP 10 to STEP 16 UNTIL i<n
• STEP 10: DEFINE j, last.
• STEP 11: last = arr[length-1]
• STEP 12: SET j=length-1. REPEAT STEP 13 and STEP 14 UNTIL j>0
• STEP 13: arr[j]= arr[j-1]
• STEP 14: j=j-1.
• STEP 15: arr[0]= last
• STEP 16: i=i+1.
• STEP 17: PRINT "Array after right rotation"
• STEP 18: SET i=0. REPEAT STEP 19 and STEP 20 UNTIL i<length
• STEP 19: PRINT arr[i]
• STEP 20: i=i+1.
• STEP 21: RETURN 0.
• STEP 22: END.

### PROGRAM:

Output:

```Original Array:
1   2   3   4   5
Array after right rotation:
3   4   5   1   2
```
Next TopicC Programs