MENU











Wednesday, 9 October 2013

Week 6 programs

Week 6
6a) Write a C++ program that uses functions.

I) to swap two integers II) to swap characters III) to swap two reals

Aim::To implement the swap functions by integer,character,real

Program::

#include< iostream >
using namespace std;
void swap(int a,int b)
{
   	cout<<"\nBefore swapping of integers:\nA="<< a<<"\nB="<< b;
	int c=a;
	a=b;
	b=c;
	cout<<"\nAfter swapping of integers:\nA="<< a<<"\nB="<< b;
}
	
voidswap(char a,char b)
{
	cout<<"\nBefore swapping of characters:\nA="<< a<<"\nB="<< b;
	char c=a;
	a=b;
	b=c;
	cout<<"\nAfter swapping of characters:\nA="<< a<<"\nB="<< b;
}

voidswap(float a,float b)
{
	cout<<"\nBefore swapping of reals:\nA="<< a<<"\nB="<< b;
	float c=a;
	a=b;
	b=c;
	cout<<"\nAfter swapping of realss:\nA="<< a<<"\nB="<< b;
}


int main()
{
	int a,b;
	cout<<"Enter two integers :";
	cin>>a>>b;
	swap(a,b);
	char c1,c2;
	cout<<"\nEnter two characters :";
	cin>>c1>>c2;
	swap(c1,c2);
	float f1,f2;
	cout<<"\nEnter two real numbers :";
	cin>>f1>>f2;
	swap(f1,f2);
}

Output::


Enter two integers :
7 17
Before swapping of integers:
A=7
B=17
After swapping of integers:
A=17
B=7

Enter two characters :
h n
Before swapping of characters:
A=h
B=n
After swapping of characters:
A=n
B=h

Enter two real numbers :
7.7 8.8
Before swapping of reals:
A=7.7
B=8.8
After swapping of reals:
A=8.8
B=7.7

No comments:

Post a Comment