In this section you will learn how to swap two numbers in c without using third variable. We will use two variable for swapping the numbers. In this program we are assigning the default value to variable.
Swapping mean interchange of the values.
Let’s suppose a user will input two number as a ‘10’ and ‘20’ then we will perform swapping operation on it. We will use only two variables for swapping the numbers.
#include<stdio.h> #include<conio.h> //SWAP OF TWO NUMBER PROGRAM USING TWO VARIABLES void main() { int num = 10, num1 = 20; clrscr(); printf("BEFORE SWAPING OF TWO NUMBER :- %d %d",num,num1); num = num + num1; //30 num1 = num - num1; //10 num = num - num1; printf("AFTER SWAPING OF TWO NUMBER :- %d %d",num,num1); getch(); }
BEFORE SWAPING OF TWO NUMBER :- 10 20 AFTER SWAPING OF TWO NUMBER :- 20 10