/* Program to SWAP two variable without using THIRD variable in C++ (with function) */
/*In this program will will not use third variable to swap the the variable but we will be using the operator to do so. Here we will be using a swap function to swap the value but it will be done by using call by reference.*/
/* Program to swap the value without using the third variable*/
//Header File
#include<iostream.h>
void swap(int &a,int &b);
int main()
{
int a,b;
cout<<"\n Enter First No:- ";
cin>>a;
cout<<"\n Enter Second No:- ";
cin>>b;
cout<<"\n Value before swaping. a= "<<a<<" b="<<b;
swap(a,b);
cout<<"\n Value after swaping. a= "<<a<<" b="<<b;
return 0;
}
void swap(int &a,int &b) //CALL BY REFERNCE
{
a=a+b;
b=a-b;
a=a-b;
}
/* Program to swap the value without using the third variable*/
//Header File
#include<iostream.h>
void swap(int &a,int &b);
int main()
{
int a,b;
cout<<"\n Enter First No:- ";
cin>>a;
cout<<"\n Enter Second No:- ";
cin>>b;
cout<<"\n Value before swaping. a= "<<a<<" b="<<b;
swap(a,b);
cout<<"\n Value after swaping. a= "<<a<<" b="<<b;
return 0;
}
void swap(int &a,int &b) //CALL BY REFERNCE
{
a=a+b;
b=a-b;
a=a-b;
}
0 comments:
Post a Comment