Write a C Program to Swap the values of two variables



Swap the values of two variables, without using third variable, Sumit Kar, Timus Rak, XOR








/*    Swap the values of two variables using three variable   */






#include<stdio.h>


void main()


{


 int a;


 int b;


 int c;





 printf("Type value of A : ");


 scanf("%i",&a);


 printf("\nType value of b : ");


 scanf("%i",&b);


 c=a;


 a=b;


 b=c;


 printf("A : %i",a);


 printf("\nb : %i",b);


}











/*    Swap the values of two variables without using third variable   */






#include<stdio.h>


void main()


{


 int a;


 int b;








 printf("Type value of A : ");


 scanf("%i",&a);


 printf("\nType value of b : ");


 scanf("%i",&b);


 a=a+b;


 b=a-b;


 a=a-b;


 printf("A : %i",a);


 printf("\nb : %i",b);


}











/* Swap values of two variables using XOR */ 






#include<stdio.h>





void main()


{


 int a;


 int b;


 printf("Type value of A : ");


 scanf("%i",&a);


 printf("\nType value of b : ");


 scanf("%i",&b);


  a ^= b;


  b ^= a;


  a ^= b;


 printf("A : %i",a);


 printf("\nB : %i",b);


}


0 Comments