Javatpoint Logo
Javatpoint Logo

3. C program to left rotate the elements of an array

In this program, we need to rotate the elements of an array towards the left by the specified number of times. In the left rotation, each element of the array will be shifted to its left by one position and the first element of the array will be added to end of the list. This process will be followed for a specified number of times.

C program to left rotate the elements of an array

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

ALGORITHM:

  • STEP 1: START
  • STEP 2: INITIALIZE arr[] ={1, 2, 3, 4, 5 }.
  • STEP 3: length = sizeof(arr1)/sizeof(arr1[0])
  • STEP 4: length = sizeof(arr)/sizeof(arr[0])
  • STEP 5: SET n =3
  • STEP 6: SET i=0. REPEAT STEP 7 to 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, first.
  • STEP 11: first = arr[0]
  • STEP 12: SET j=0. REPEAT STEP 13 to STEP 14 UNTIL j<length-1.
  • STEP 13: arr[j]= arr[j+1]
  • STEP 14: j=j+1.
  • STEP 15: arr[j]= first
  • STEP 16: i=i+1.
  • STEP 17: PRINT new line.
  • STEP 18: PRINT "Array after left rotation"
  • STEP 19: ENDSET i=0. REPEAT STEP 20 to STEP 21 UNTIL i<length
  • STEP 20: PRINT arr[i]
  • STEP 21: i=i+1.
  • STEP 22: RETURN 0.
  • STEP 23: END

PROGRAM:

Output:

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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA