C Program:
#include<stdio.h>
void main()
{
int x,y,temp;
printf("Enter the value of xand y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\n x= %d\n y = %d\n",x,y);
temp = x;
x= y;
y = temp;
printf("After Swapping\n x= %d\ny= %d\n",x,y);
}
#include<stdio.h>
void main()
{
int x,y,temp;
printf("Enter the value of xand y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\n x= %d\n y = %d\n",x,y);
temp = x;
x= y;
y = temp;
printf("After Swapping\n x= %d\ny= %d\n",x,y);
}
#include<stdio.h>
void main(){
int a=5,b=10;
a=b+a; //now a =15
b=a-b; //now b= 5
a=a-b; // now a=10 (done)
printf("a= %d\nb= %d",a,b);
}
#include<stdio.h>
void main(){
int a=10,b=30;
a=a+b-(b=a);
//10+30-(10) a becomes 30
// b is assigned with 10 in the same line of code
printf("a= %d\nb= %d",a,b);
}
#include<stdio.h>
void main(){
int a=8,b=12;
/* a: 8 : 01000
b:12 : 01100
*/
a=a^b;
/* 01000 ^
01100
a= 00100 (4)
*/
b=a^b;
/* 00100 ^
01100
b= 01000 (8)
*/
a=b^a;
/* 01000 ^
00100
a= 01100 (12)
*/
printf("a= %d\nb= %d",a,b);
}
#include<stdio.h>
void main(){
int a=14,b=4;
/* a: 14 : 1110
b: 4 : 0100
~a: : 0001
~b: : 1011
*/
a=b-~a-1;
/* b: 0100 -
~a: 0001
0011 -
1: 0001
0010
now a= 0010
*/
b=a+~b+1;
/* a: 0010 +
~b: 1011
1: 0001
now b: 1110 (14)
~b: 0001
*/
a=a+~b+1;
/* a: 0010 +
~b: 0001
1: 0001
now a: 0100 (4)
*/
printf("a= %d\nb= %d",a,b);
}