Program for array rotation( Method 2 - One by one Rotation)

  Method 2 - One by one Rotation:


 Code in C:



#include<stdio.h>
int main(){
    int arr[50],n,d,temp;
    printf("Array Length:");
    scanf("%d",&n);
    printf("Rotation:");
    scanf("%d",&d);
    printf("Enter %d Array:",n);
    for(int a=0;a<n;a++){
        scanf("%d",&arr[a]);
    }
    printf("\n Before Rotation:\n");
    for(int a=0;a<n;a++){
        printf("%d  ",arr[a]);
    }
    for(int i=0;i<d;i++){
        temp=arr[0];
        for(int j=0;j<n-1;j++){
             arr[j]=arr[j+1];
            }
        arr[n-1]=temp;
    }
    printf("\nAfter Rotation:\n");
    for(int a=0;a<n;a++){
        printf("%d  ",arr[a]);
    }
    return 0;
}






C:\Users\Rakesh> gcc .\arrayRotation02.c
 C:\Users\Rakesh> ./a

OUTPUT:


Array Length:7
Rotation:2
Enter 7 Array:3
4
6
7
8
2
9

 Before Rotation:
3  4  6  7  8  2  9
After Rotation:
6  7  8  2  9  3  4















Comments