Rotate the array in C++ by R4R Team

Array:

Array is the collection of different values having same data type.
-We have a integer Array that having all integer values.
-Character array is known as String.

Rotation of array:

Example-
Integer Array is {1,2,3,4,5}
Rotated Array is {5,4,3,2,1}


program:

#include < iostream>
using namespace std;
int main()
{
int a[20],b[20],i,n;
cout<<"Number of element in array?"<< endl;
cin>>n;
cout<<"Enter element in array"<< endl;
//input in first array
for(i=0;i< n;i++)
{
cin>>a[i];
}
//Now reverse and copy in another array
for(i=0;i< n;i++)
{
b[i]=a[n-i-1];
}
//Now display second array
cout<<"Rotated Array is: "<< endl;
for(i=0;i< n;i++)
{
cout<< b[i]<<" ";
}
return 0;
}


output-

Number of element in array?
5
Enter element in array
4 3 1 5 2
Rotated array is:
2 5 1 3 4




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!