About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Wednesday, September 4, 2013

C Program to Swap two no’s without using third variable

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b;

clrscr();

printf("\nEnter value for num1 & num2 : ");
scanf("%d %d",&a,&b);

a=a+b;
b=a-b;
a=a-b;

printf("\nAfter swapping value of a : %d",a);
printf("\nAfter swapping value of b : %d",b);

getch();
}
Output :
Enter value for num1 & num2 : 10 20

After swapping value of a : 20
After swapping value of b : 10

1 comment:

zzzzz said...

Thanks for sharing this nice C program. Here is another way in C to swap two numbers without using temporary variable. We can use bitwise XOR operator to swap two variables.