Showing posts with label swapping. Show all posts
Showing posts with label swapping. Show all posts

Monday, May 9, 2016

c++ program to swap two numbers using third variable

problem statement:- swapping of two numbers means you are given two numbers in variables a and b respectively and with the use program you have to swap value of a to b and value of b to a

#include<iostream.h>
class a
{
public:
int a,b,c;
void swap();
};
void a::swap()
{

c=a;
a=b;
b=c;
cout<<"\n after swapping\n";
cout<<a<<" "<<b;
}
void main()
{
 a obj;
 cout<<"enter two nos.\n";
 cin>>obj.a>>obj.b;
 cout<<"before swapping\n";
 cout<<obj.a<<" "<<obj.b;
 obj.swap();
 }

output
enter two nos
13 18
before swapping
13 18
after swapping
18 13

Tuesday, April 5, 2016

C++ program to implement swapping of two numbers

#include<iostream.h>
class a
{
public:
int a,b,c;
void swap();
};
void a::swap()
{

 c=a;a=b;b=c;
cout<<"\n after swapping\n";
cout<<a<<" "<<b;
}
void main()
{
 a obj;
 cout<<"enter two nos.\n";
 cin>>obj.a>>obj.b;
 cout<<"before swapping\n";
 cout<<obj.a<<" "<<obj.b;
 obj.swap();
 }
output
enter two nos 5 8
before swapping
5 8
after swapping 5 8