Write a C Program to Swap the values of two variables
![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](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd7JUeFcGURR9T3358Yzicn60HdqsMSKqXUO3-Sxfx4K5Sqc7UgUwbzWzXCCtqRS4lZ_KrO_J8XbTe1fxRFzDQ-ZO_BYWAYQLuUNCKozi7hAbEpi0FGxITpJhJtEywftgs9V5CX1CA6O8a/s1600/c-programming-tutorial.jpg)
/* 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
Post a Comment