// C program to rotate
//Array by k elements
#include <stdio.h>

//Print Array
void printArray(int arr[],int n)
{
    int i;
    for(i=0;i<n;i++)
        printf("%d",arr[i]);
}

//Calculate greatest common factor
int gcd(int a,int b)
{
    if(b==0)
       return a;
    else
        return gcd(b,a % b);
}

//Rotate array
void Rotate(int arr[],int K,int N)
{
    int i,j,a,temp;
    K=K%N;

    int rotate=gcd(K,N);

    for(i=0;i<rotate;i++){
        
        temp=arr[i];
        j=i;
        while (1){
            a=j+K;
            if(a>=N)
               a=a-N;
            if(a==i)
               break;
            arr[j]=arr[a];
            j=a;
        }
        arr[j]=temp;        
    }
}

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

    //Rotate array
    Rotate(arr,2,5);

    //Printing array
    printArray(arr,5);

    return 0;
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: