WRITE A PROGRAM THAT SWAP (EXCHANGE) THE VALUE OF TWO VARIABLES.
#include<stdio.h> // Header file “Standard Input and Output”
#include<conio.h> //Header file “Console input and output”
void main ()
{
clrscr(); // Clear the output
screen
int a, b, c;
a = 15;
b
= 25;
c
= a; // The
value of a copied in c
a
= b; // the
value of a is exchanged by b
b
= c; // and
value of b is exchanged by c
printf(“After
swapping the values of a and b is %d, %d“, a, b);
getch();
}
OUTPUT
After swapping
the values of a and b is 25 15
EXPLANATION
Initially we
have taken two variables “a” and “b” and gave initial values 15 and 25
respectively. In the second step we have swapped (exchange) the values of
variable “a” and “b” by using another variable “c”. For swapping values of any
two variables we always use an extra variable.
No comments:
Post a Comment