Exchange the values of two variables without using an extra variable
Exchange the values of two variables and do not use the third variable
#include<stdio.h>
int main()
{
int a,b;
printf("Please input two intergers:\n");
scanf("%d%d",&a,&b);
printf("a is %d, b is %d\n",a,b);
a = a + b;
b = a - b;
a = a - b;
printf("Now a is %d, b is %d\n",a,b);
}
Another method:
Exchange the values of two variables and do not use the third variable
#include<stdio.h>
int main()
{
int a,b;
printf("Please input two intergers:\n");
scanf("%d%d",&a,&b);
printf("a is %d, b is %d\n",a,b);
a = a^b;
b = a^b;
a = a^b;
printf("Now a is %d, b is %d\n",a,b);
}
5 3
a is 5, b is 3
Now a is 3, b is 5