Monday, 8 December 2014

Program to swap two numbers using 3 different methods
#include<stdio.h>
#include<stdlib.h>
main()
{
int a,b;
printf("swap by using XOR\n");
printf("enter values for a and b\n");
scanf("%d%d",&a,&b);
a^=b^=a^=b;
printf("a=%d\tb=%d\n",a,b);
manip();
temp();
}
manip()
{
int a,b;
printf("swap 2 numbers by manipulation\n");
printf("enter values for a and b\n");
scanf("%d%d",&a,&b);
b=(a*b)/(a=b);
printf("a=%d\t b=%d\n",a,b);
}
temp()
{
int a,b,temp;
printf("swap 2 numbers by Temp Variable\n");
printf("enter values for a and b\n");
scanf("%d%d",&a,&b);
temp=a;
a=b;
b=temp;
printf("a=%d\tb=%d\n",a,b);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex56.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
swap by using XOR
enter values for a and b
10
20
a=20 b=10
swap 2 numbers by manipulation
enter values for a and b
12
32
a=32  b=12
swap 2 numbers by Temp Variable
enter values for a and b
32
54
a=54 b=32