Write a C C++ program to swap values of two variables

 

Flowchart

Swapping values of two variables flowchart

 

Coding in C

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

 

Coding in C++

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

 

Output

Swapping values of two variables output

 

Published by

admin

I am a computer teacher, programmer and web developer