C Program To Right Rotate The Elements Of An Array - C Programming Tutorial
C Course / Programs / C Program To Right Rotate The Elements Of An Array

C Program To Right Rotate The Elements Of An Array

BLUF: Understanding C Program To Right Rotate The Elements Of An Array is a foundational part of learning C programming. This tutorial explains the core principles and syntax needed to implement this concept effectively.
Core Programming Principle: C Program To Right Rotate The Elements Of An Array

C provides direct access to memory and system resources. Learn how C Program To Right Rotate The Elements Of An Array leverages this power in the lesson below.

If the value of n is 1, each element in the array will shift one position to the right. Consequently, the initial first element will now occupy the second position, the second element will shift to the third position, and this pattern continues until the last element wraps around to become the new 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:

Example

#include <stdio.h>  

   

int main()  

{  

    //Initialize array   

    int arr[] = {1, 2, 3, 4, 5};   

    //Calculate length of array arr  

    int length = sizeof(arr)/sizeof(arr[0]);  

    //n determine the number of times an array should be rotated  

    int n = 3;  

      

    //Displays original array  

    printf("Original array: \n");  

    for (int i = 0; i < length; i++) {   

        printf("%d ", arr[i]);   

    }   

      

    //Rotate the given array by n times toward right  

    for(int i = 0; i < n; i++){  

        int j, last;  

        //Stores the last element of the array  

        last = arr[length-1];  

      

        for(j = length-1; j > 0; j--){  

            //Shift element of array by one  

            arr[j] = arr[j-1];  

        }  

        //Last element of array will be added to the start of array.  

        arr[0] = last;  

    }  

      

    printf("\n");  

      

    //Displays resulting array after rotation  

    printf("Array after right rotation: \n");  

    for(int i = 0; i< length; i++){  

        printf("%d ", arr[i]);  

    }  

    return 0;  

}

Output:

Output

Original Array:

1   2   3   4   5

Array after right rotation:

3   4   5   1   2

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience