0

Swapping two variables without and with using third variable.

#include<stdio.h>

#include<conio.h>

void swap1(int,int);

void swap2(int,int);

void main()

{

int a,b;

clrscr();

printf(“Enter the values for a & b respectively:”);

scanf(“%d%d”,&a,&b);

swap1(a,b);

swap2(a,b);

getch();

}

void swap1(int a,int b)

{int c;

printf(“\n Swap using third variable:”);

c=a;

a=b;

b=c;

printf(“\n After swapping, value of a is %d & value ofb is %d”,

a,b);

}

void swap2(int a, int b)

{

printf(“\n Swap without using third variable:”);

a=a+b;

b=a-b;

a=a-b;

printf(“\n After swapping,value of a is %d & value of b is %d “,a,b);

}

ks007

Leave a Reply

Your email address will not be published. Required fields are marked *