C Program to Right Rotate Array Elements

C Program to Right Rotate Array Elements

C program to right rotate an array; Through this tutorial, we will learn how to right rotate an array using standard method and function in c programs.

Programs to Right Rotate Array Elements in C

Checkout the following program to right rotate an array using standard method and function in c:

  • C Program to Right Rotate Array Elements using Standard Method
  • C Program to Right Rotate Array Elements using Function

C Program to Right Rotate Array Elements 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 right rotate : ");
    scanf("%d", &k);
     
    for(i=0; i<k; i++)
    {
        temp=a[n-1];
        for(j=n-1; j>0; j--)
        {
           a[j]=a[j-1];
		}
 
         a[j]=temp;
        
 
    }
    printf("\narray elements after right rotate  : ");
 
    for(i=0; i<n; i++)
    {
       printf("%d ",a[i]);
    }
    
 }

The output of the above c program; as follows:

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

C Program to Right Rotate Array Elements using Function

#include <stdio.h>
 
int rightrotate(int *a,int n,int k)
{ 
    int i,j,temp;
    for(i=0; i<k; i++)
    {
        temp=a[n-1];
        for(j=n-1; j>0; j--)
        {
           a[j]=a[j-1];
		}
 
         a[j]=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 right rotate : ");
    
	scanf("%d", &k);
     
    rightrotate(a,n,k);
   
    printf("\narray elements after right rotate  : ");
 
    print(a,n);
 
     
    
}

The output of the above c program; as follows:

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *