Below is the program for swapping variables without any extra variable.
[sourcecode language='c']
#include "stdio.h"
#include "conio.h"
void main()
{
int a=7;b=5;
clrscr();
a=a+b;
b=a-b;
a=a-b;
getch();
}
[/sourcecode]
The above code in a single line
[sourcecode language='c']
void main()
{
int n1=5,n2=8;
printf("Before swapping number 1=%d and number 2=%d",n1,n2);
n2=(n1+n2)-(n1=n2); //swapping code
printf("After swapping number 1=%d and number 2=%d",n1,n2);
}
[/sourcecode]



0 comments:
Post a Comment