Interchange an element using One Dimensional Array in C


This program is about interchange and print the element in an array using array.

Source Code

#include<stdio.h>
int main()
{
  int i,j,a[10],n,m,l;
  printf("\nEnter the limit:");
  scanf("%d",&n);
  printf("\nEnter the values:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("\nGiven values are:");
  for(i=0;i<n;i++)
  {
    printf("\na[%d]=%d",i,a[i]);
  }
  l=n%2==0?n:n-1;
  for(i=0;i<l;i=i+2)
  {
    m=a[i];
    a[i]=a[i+1];
    a[i+1]=m;
  }
  printf("\nInterchanged value is:");
  for(i=0;i<n;i++)
  {
    printf("\na[%d]=%d",i,a[i]);
  }
  return 0;
}
To download raw file Click Here

Output

Enter the limit:5
Enter the values:1
2
3
4
5
Given values are:
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
Interchanged values
a[0]=2
a[1]=1
a[2]=4
a[3]=3
a[4]=5

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C


Learn All in Tamil © Designed & Developed By Tutor Joes