Shift single dimensional array


C Program to shift elements of a single dimensional array in the right direction by one position. If the given array is 76 35 43 22 then after the execution of the program, it should become 22 76 35 43.


#include  <stdio.h>
#include  <conio.h>

void  main()
{
  int  i , n , t , x[50] ;

  clrscr() ;

  printf("Enter the number of elements: ") ;
  scanf("%d" , &n) ;
  printf("Enter the elements: \n") ;
  for(i=0 ; i<n ; i++)
    scanf("%d" , &x[i]) ;

  printf("Original array is as shown: \n") ;
  for(i=0 ; i<n ; i++)
    printf("%d " , x[i]) ;
  printf("\n") ;

  /* shifting array elements */
  t=x[n-1] ;
  for(i=n-1 ; i>=1 ; i--)
    x[i]=x[i-1] ;
  x[0]=t ;

  printf("New array is as shown: \n") ;
  for(i=0 ; i<n ; i++)
    printf("%d " , x[i]) ;
 
  getch();
}


Output :



Enter the number of elements: 6
Enter the elements: 
2 1 4 5 6 8
Original array is as shown: 
2 1 4 5 6 8 
New array is as shown: 
8 2 1 4 5 6