C Program To Left Rotate An Array

In this tutorial, i am going to show you how to left rotate an array with the help of standard method and function in c programs.

All C Programs To Left Rotate An Array

  • C Program To Left Rotate An Array using Standard Method
  • C Program To Left Rotate An Array using Function

C Program To Left Rotate An Array using Standard Method

#include <stdio.h>
 
int main()
{
    int a[10000],i,n,j,k,temp;
   
    printf("Enter size of the  array : ");
    scanf("%d", &n);
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    printf("how many times left rotate : ");
    scanf("%d", &k);
     
    for(i=0; i<k; i++)
    {
        temp=a[0];
        for(j=0; j<n-1; j++)
        {
           a[j]=a[j+1];
		}
 
         a[n-1]=temp;
        
 
    }
    printf("\narray elements after left rotate  : ");
 
    for(i=0; i<n; i++)
    {
       printf("%d ",a[i]);
    }
    
 }

The result of the above c program; as follows:

Enter size of the  array : 5
Enter elements in array : 1 2 34 5 4
how many times left rotate : 2
array elements after left rotate  : 34 5 4 1 2 

C Program To Left Rotate An Array using Function

#include <stdio.h>
 
int leftrotate(int *a,int n,int k)
{ 
    int i,j,temp;
    for(i=0; i<k; i++)
    {
        temp=a[0];
        for(j=0; j<n-1; j++)
        {
           a[j]=a[j+1];
		}
 
         a[n-1]=temp;
    }
    
       
 }
 print(int *a,int n)
{ 
   
    int i;
    for(i=0; i<n; i++)
    {
       printf("%d ",a[i]);
    }
    
       
 }
 
  
int main()
{
    int a[10000],i,n,j,k,temp;
   
    printf("Enter size of the  array : ");
    scanf("%d", &n);
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    printf("how many times left rotate : ");
    scanf("%d", &k);
     
    leftrotate(a,n,k);
   
    printf("\narray elements after left rotate  : ");
 
      print(a,n);
 
     
    
}  

The result of the above c program; as follows:

Enter size of the  array : 5
Enter elements in array : 45 25 62 47 58
how many times left rotate : 3
array elements after left rotate  : 47 58 45 25 62 

More C Programming Tutorials

Leave a Comment