C Program to Swap Two Arrays Without Using Temp Variable

C program to swap two arrays without using temp variable; Through this tutorial, we will learn how to swap two arrays without using temp variable in the c program.

Programs to Swap Two Arrays Without Using Temp Variable

To swap two arrays without using temp variable in the c program:

  • C Program to Swap Two Arrays Without Using Temp Variable Using For Loop
  • C Program to Swap Two Arrays Without Using Temp Variable Using Bitwise Operator

C Program to Swap Two Arrays Without Using Temp Variable Using For Loop

/* C Program to Swap Two Arrays Without Using Temp Variable */
#include<stdio.h>

int main()
{
 int Size, i, a[10], b[10], Temp[10];

 printf("\nPlease Enter the Size of the Array\n");
 scanf("%d", &Size);

 printf("\nPlease Enter the First Array Elements\n");
 for(i = 0; i < Size; i++)
  {
      scanf("%d", &a[i]);
  }

 printf("\nPlease Enter the Second Array Elements\n");
 for(i = 0; i < Size; i ++)
  {
      scanf("%d", &b[i]);
  }

 //Swapping two Arrays
 for(i = 0; i < Size; i++)
  {
    a[i] = a[i] + b[i];
    b[i] = a[i] - b[i];
    a[i] = a[i] - b[i];
  }

 printf("\n a[%d] Array Elements After Swapping \n", Size);
 for(i = 0; i < Size; i ++)
  {
      printf(" %d \t ",a[i]);
  }

printf("\n\n b[%d] Array Elements After Swapping \n", Size);
 for(i = 0; i < Size; i ++)
  {
      printf(" %d \t ",b[i]);
  }

  return 0;
} 

The output of the above c program; as follows:

Please Enter the Size of the Array
5
Please Enter the First Array Elements
1 2 3 4 5
Please Enter the Second Array Elements
6 7 8 9 10
a[5] Array Elements After Swapping
 6 	  7 	  8 	  9 	  10

 b[5] Array Elements After Swapping
 1 	  2 	  3 	  4 	  5 

C Program to Swap Two Arrays Without Using Temp Variable Using Bitwise Operator

/* C Program to Swap Two Arrays Without Using Temp Variable */

#include<stdio.h>

int main()
{
 int Size, i, a[10], b[10], Temp[10];

 printf("\nPlease Enter the Size of the Array\n");
 scanf("%d", &Size);

 printf("\nPlease Enter the First Array Elements\n");
 for(i = 0; i < Size; i++)
  {
      scanf("%d", &a[i]);
  }

 printf("\nPlease Enter the Second Array Elements\n");
 for(i = 0; i < Size; i ++)
  {
      scanf("%d", &b[i]);
  }

 //Swapping two Arrays
 for(i = 0; i < Size; i++)
  {
    a[i] = a[i] ^ b[i];
    b[i] = a[i] ^ b[i];
    a[i] = a[i] ^ b[i];
  }

 printf("\n a[%d] Array Elements After Swapping \n", Size);
 for(i = 0; i < Size; i ++)
  {
      printf(" %d \t ",a[i]);
  }

printf("\n\n b[%d] Array Elements After Swapping \n", Size);
 for(i = 0; i < Size; i ++)
  {
      printf(" %d \t ",b[i]);
  }

  return 0;
} 

The output of the above c program; as follows:

Please Enter the Size of the Array
5
Please Enter the First Array Elements
1 2 3 5 6
Please Enter the Second Array Elements
10 20 30 40 50
a[5] Array Elements After Swapping
 10 	  20 	  30 	  40 	  50

 b[5] Array Elements After Swapping
 1 	  2 	  3 	  5 	  6 	

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *