"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Friday, January 29, 2010

Swap 3 integers without any fourth variables

eg.                               x = 10         y = 20     z = 30
output :                    x = 30         y = 10     z =20


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

void main()
{
int x, y, z;

cout<< "\n Enter 3 nos :";
cin>> x >> y >> z ;

cout<<"\n Old values :\n";
cout << "x = " << x << " y = " <<< "z = " << z ;

x = x+y+z;
y = x - (y+z);
z = x - (y+z);
x = x - (y + z);

cout<<"\n New values :\n";
cout << "x = " << x << " y = " <<< "z = " << z ;

getch();
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.