Programming for Problem Solving

GTU Practical 44

44. Write a functioning Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function.
#include<stdio.h>  ( this applies in the given code )

#include
void swap(int *, int *);

int main()
{
int i=5,j=8;
printf(“\n Values Before Exchange :”);
printf(“\n i = %d j = %d”,i,j);
swap(&i,&j);
printf(“\n Values After Exchange :”);
printf(“\n i = %d j = %d”,i,j);
return 0;
}

void swap(int *a,int *b)
{
*a = *a + *b;
*b = *a – *b;
*a = *a – *b;
}

OUTPUT

Values Before Exchange :
i = 5 j = 8
Values After Exchange :
i = 8 j = 5

GTU STUDY