Sunday 29 July 2012

WAP to Swap two number without any function.

Swap two number means , simply interchange the value,
i.e. if
a=2
b=5
after swapping 
a=5
b=2
 to write a program to implement this logic, there a various ways, I am describing simplest one.
take a temporary variable, say temp , 
store value of a into it i.e. temp=a
now store value of b into a i.e. a=b
now store value of temp into b i.e. b=temp 
by these three step we can easily swap the value of a & b


let us take an example
let a=2, b=5 and temp=anything,
now we need to swap the value of a & b
therefor follow above three steps
1.   temp=a     i.e. temp=2
2.   a=b          i.e. a=5
3.   b=temp   i.e. b=2

congrats swapping is done.

Now Implementing this logic into codes

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

void main( )
{
int a, b, temp;            // variable declaration
cout<<"Enter 1st Variable\t";
cin>>a;
cout<<"\nEnter 2nd Variable\t";
cin>>b;
temp=a;
a=b;
b=temp;
cout<<"\nSwapped Value of 1st variable\t"<<a;
cout<<"\nSwapped value of 2nd variable\t"<<b;
cout<<"\n\n\t\t\tProgram Ends";
getch();
}

Programming Guru

1 comment:

This Blog is not managed by anyone, If you are interested then Contact me avinashkgec@gmail.com

Related Post