Swap / Interchange two variables [numbers] without using Third Variable

January 10, 2025 No Comments » Hits : 182





Generally Swaping two number requires three variables , Let’s Take look at Procedure of swaping two Number
Let -

int num1=20,num2=40,temp;

For Swaping Two numbers following procedure is used -

temp = num1;
num1 = num2;
num2 = temp;

Here is Program for : [Swap / Interchange two variables [numbers] without using Third Variable]

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
printf("\nEnter First Number : ");
scanf("%d",&num1);
printf("\nEnter Second Number : ");
scanf("%d",&num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
printf("\nNumbers after Exchange : ");
printf("num1 = %d and num2 = %d",num1,num2);
getch();
}

Output :

Enter First Number : 20
Enter Second Number : 40
Numbers after Exchange : num1 = 40 and num2 = 20

Incoming search terms: