Thursday, September 3, 2015

Swap of Two Numbers

/*Program in C to Swap the value of two variables without using extra variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter two numbers");
scanf("%d%d",&a,&b);
printf("\nBefore Swap");
printf("\na=%d   b=%d",a,b);
a=a+b;
b=b-a;
a=b-a;
printf("\nAfter Swap");
printf("\na=%d   b=%d",a,b);
getch();
}

Output
Enter two numbers 3
5
Before Swap
a=3   b=5
After Swap
a=5   b=3

No comments: