Write a C C++ program to swap without using third variable

 

Flowchart

Swapping without using third variable flowchart

 

Coding in C

#include<stdio.h>
int main()
{
int a,b;
printf(“Enter a b: “);
scanf(“%d%d”,&a,&b);
a = a+b;
b = a-b;
a = a-b;
printf(“a = %d b = %d”,a,b);
return 0;
}

 

Coding in C++

#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<“Enter a b: “;
cin>>a>>b;
a = a+b;
b = a-b;
a = a-b;
cout<<“a = “<<a<<” b = “<<b;
return 0;
}

 

Output

Swapping without third variable output

Published by

admin

I am a computer teacher, programmer and web developer