Computer Science, asked by akash429, 1 year ago

c++ swap program write

Answers

Answered by KshitijMaurya
2
hey there,

There are 3 methods of writing c++ swap programme one is using temporary variables and the other is without using temporary variables and other is using third variable.

Here I am writing program with non temporary variables

let's begin

#include <iostream>
using namespace std;

int main( )
{
int a = 5, b = 10;

cout << "Before swapping." << endl;

cout << "a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}

Answered by Anonymous
8
ANSWER
.................



THE PROGRAM IS GIVEN BELOW

.................






                #include

 <iostream>


 by using namespace std;






int main( )


 




int a = 5,


                                            b = 10;

cout

                                             << "Before swapping." << endl; 

                      cout << "a = " << a << ", b = "


                    << b << endl;
a = a + b; 

b = a - b;   


a = a - b; 


cout << "\nAfter swapping." << endl; 


cout << "a = " << a << ", b = " << b << endl; 


return 0; 


}




Similar questions