TECH TIPS Headline Animator

Tuesday, November 23, 2010

SWAPPING VARIABLES WITHOUT TEMP VARIABLE

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

Twitter Delicious Facebook Digg Stumbleupon Favorites More