ProAnswers.org

how to Swap two variables without using third variable.

try this…

#include
int main()
{
int a=5,b=10;

a=b+a;
b=a-b;
a=a-b;
printf("a= %d  b=  %d",a,b); 

}

Abigail you can try following code snippet also.

a=5;
b=10;
a=a^b;
b=a^b;
a=b^a;
printf("
a= %d b= %d",a,b);

a=5;
b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("

a= %d b= %d",a,b);

#include
main()
{
int a,b;
printf("
Enter two number to a & b :");
scanf("%d%d",&a,&b);
a=a^b;
b=a^b;
a=b^a;
printf("a= %d and b= %d
",a,b);

}

#include
#include
void main()
{
int a,b; // a and c are integers
clrscr(); // this is a function that used to clear the previos output
printf(“enter any two numbres
“);
scanf(”%d%d”,&a,&b);
printf("value of First number before Swap = %d
",a);
printf("value of Second number before Swap = %d
",b);
/Logic to swap**********/
a =a+b; // sum of a+b given to a
b =a-b; // diffrence of a-b given to b;
a =a-b; // diffrence of a-b given to a;
printf("value of First number after Swap = %d
",a);
printf("value of Second number after Swap = %d
",b);

getch();
}