• C program to interchange the elements of an array.
  • Now we will see how to swap two integer arrays in C programming by using third array.



Program #1: Write a C program to swap two integer arrays

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.   
  6.   int a[5],b[5],c[5],i;
  7.   printf("Enter First array 5 elemnets \n");
  8.   for(i=0;i<5;i++)
  9.   scanf("%d",&a[i]);
  10.   printf("\nEnter Second array 5 elements\n");
  11.   for(i=0;i<5;i++)
  12.             scanf("%d",&b[i]);
  13.   printf("Arrays before swapping\n");
  14.   printf("First array \n");
  15.   for(i=0;i<5;i++){
  16.             printf("%d ",a[i]);
  17.   }
  18.   printf("\nSecond array\n");
  19.   for(i=0;i<5;i++){
  20.             printf("%d ",b[i]);
  21.   }
  22.   for(i=0;i<5;i++){
  23.             c[i]=a[i];
  24.             a[i]=b[i];
  25.             b[i]=c[i];
  26.   }
  27.   printf("\nArrays after swapping\n");
  28.   printf("First array elements \n");
  29.   for(i=0;i<5;i++){
  30.             printf("%d ",a[i]);
  31.   }
  32.   printf("\nSecond array elements \n");
  33.   for(i=0;i<5;i++){
  34.             printf("%d ",b[i]);
  35.   }
  36.  return 0;
  37. }

Output:


swap arrays in c


Instance Of Java

We are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu