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);


}


Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C