Given an array from the user the task is to right shift array all elements by one place.
Example:
Consider an array of size 5.
| 2 | 6 | 4 | 9 | 7 |
Right shifted array.
| 7 | 2 | 6 | 4 | 9 |
Note: Here the last element is brought to first position of the array.
C
#include <stdio.h>
int main()
{
int n=0,temp=0;
printf("Enter the size of array: ");
scanf("%d",&n);
int ar[n];
printf("Enter the array elements:-\n");
for(int i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
temp=ar[n-1];
for(int i=n-1;i>0;i--)
{
ar[i]=ar[i-1];
}
ar[0]=temp;
printf("Right shifted array: ");
for (int i=0;i<n;i++)
{
printf("%d ",ar[i]);
}
return 0;
}C