C Online Compiler
Example: C Program to Make a Circular Rotation of an Array by K Positions
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C Program to Make a Circular Rotation of an Array by K Positions #include <stdio.h> #define N 100 int main() { int e, r, d; int arru[N]; // e is the number of array elements // r is the number of rotations of the array // d is the number of indexes to display // arru is the array container printf("-----Enter the number of elements of the array-----\n"); scanf("%d", &e); printf("\n-----Enter the number of rotations of the array-----\n"); scanf("%d", &r); printf("\n-----Enter the number of indexes to be displayed-----\n"); scanf("%d", &d); printf("\n-----Input the array elements-----\n"); for (int i = 0; i < e; i++) { scanf("%d", &arru[i]); } printf("\n\n------The Array Elements are------\n"); for (int i = 0; i < e; i++) { printf("%d ", arru[i]); } printf("\n"); // Remove the number of full array rotations from k r %= e; for (int i = 0; i < d; i++) { int in; printf("\n------Enter the index of the array to be displayed------\n"); scanf("%d", &in); printf("\n------The element in the array is------\n"); // Calculate the new index printf("%d\n", arru[(e + in - r) % e]); } return 0; }
4 2 2 12 34 56 43 2 3
Output
Clear
ADVERTISEMENTS