"KEEP LEARNING, DON'T STOP."
"KEEP LEARNING, DON'T STOP."
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 ‘50’ and ‘30’ then we will perform swapping operation on it. We will use only two variables for swapping the numbers.
#include<iostream.h> #include<conio.h> //SWAPPING OF TWO NUMBER PROGRAM USING TWO VARIABLES void main() { int num = 50, num1 = 30; clrscr(); cout<lt;"BEFORE SWAPPING OF TWO NUMBER:- "<lt;num<lt;" "<lt;num1; num = num + num1; // 80 num1 = num - num1; //30 num = num - num1; cout<lt;"\nAFTER SWAPPING OF TWO NUMBER:- "<lt;num<lt;" "<lt;num1; getch(); }
BEFORE SWAPING OF TWO NUMBER :- 50 30 AFTER SWAPING OF TWO NUMBER :- 30 50