C++ program to interchange the values of two variables without using third variable

Refer the below images for code and outputs and code will be provided just below it
fig1. Code
fig2. Output of Code


 /* Write a C++ program to interchange the values of two variables without using third variable*/

#include <iostream> /* BASIC BUT IMPORTANT HEADER FILE*/

#include <stdio.h>

using namespace std; /* FOR STANDARD INPUT OUTPUT */

int main()

{

int a,b; /* DECLARING VARIABLES AND THEIR DATA TYPES*/

    cout << "HELLO EVERYONE, in this program we are interchanging the values of two variables without using third variable with the help of C++ lang. \n\n" ;

cout << "\n enter the values for a and b: \n";

cout << "-----------------------------------\n";

cin >>a>>b;

cout << "Before interchange: A = " << a << " and B = " << b << "\n";

a = a + b;

b = a - b;

a = a - b;

cout << "After interchange: A = " << a << " and B = " << b << "\n";

getc ;

}



Comments