Swap item in Array by R4R Team

Example-
Array is {3,2,5,6,0,1}
After swap {2,3,6,5,1,0}

program-

#include< stdio.h>
#include< math.h>
int main()
{
int a[100],n,i,t;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d number in array\n",n);
//input in array
for(i=0;i< n;i++)
{
scanf("%d",&a[i]);
}
//Swap the Adjacent numbers
for(i=0;i< n;i=i+2)
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
//Now traverse
printf("Now Array is\n");
for(i=0;i< n;i++)
{
printf("%d,",a[i]);
}
}


output-

Enter number of elements
6
Enter 6 number in array
1 2 3 4 5 6
Now Array is
2,1,4,3,6,5,

-In this program, we take a input of the number in the Array.
-We run the loop and increase the value of i by 2 (i=i+2)
-We use third variable to swap the items in array.




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!