This program is written in C programming language. It is used to interchange the elements of an array. The program takes input from the user for the limit of the array and the values of the elements in the array. It then prints out the given values of the array.
The program then uses a for loop to iterate through the array and interchanges the elements at even and odd positions. The new array with interchanged elements is then printed out. The ternary operator ( ? : ) is used to check whether the limit of the array is even or odd. If the limit is even, the loop iterates through all the elements of the array. But if the limit is odd, the loop iterates through all the elements except the last element of the array.
#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
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
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions