Wednesday, 25 May 2016

PROGRAM TO SWAP NUMBERS

#include<iostream.h>

#include<conio.h>


void main()

{

clrscr();                        \\this will clear the previous output generated

int a,b;                           \\ this is know as variable declaration. 'int' is a data type(a keyword)

int c;                                               \\another variable declaration

cout<<"Enter the value of a";   \\this command will generate the message as it is on the screen

cin>>a;         \\this is taking input from the user

cout<<"Enter the value of b"; \\this command will generate the message as it is on the screen

cin>>b;          \\this is taking input from the user



cout<<"The entered value of a and b is";

cout<<a<<"\t"<<b;                          

c=b;                                                  \\we introduced a variable 'c' to copy contents of b to c

b=a;                                                    then a to b......now value of a is transferred to b

a=c;                                                   then again c to a(c contains value of b)

                                                        \\ in short we used another variable (c) to exchange contens.

cout<<"The new value after swap is ";

cout<<a<<"\t"<<b;

getch();                                              

}

No comments:

Post a Comment