Program to swap 2 variable values using third variable
- Store value of 1st variable in third variable that is temporary
- Store value of 2nd variable in 1st variable
- Now store value of temp or third variable in 2nd
C Code
#include <stdio.h>
#include <conio.h>
int main()
{
int var1, var2, temp;
printf("Enter value for first integer: ");
scanf("%d", &var1);
printf("Enter value for second integer: ");
scanf("%d", &var2);
printf("\nValues Before swapping\n");
printf("First Integer: %d", var1);
printf("\nSecond Interger: %d", var2);
temp = var1;
var1 = var2;
var2 = temp;
printf("\n\nValues After swapping\n");
printf("First Integer: %d", var1);
printf("\nSecond Interger: %d", var2);
getch();
return 0;
}
C++ Code
#include <iostream>
using namespace std;
int main()
{
int var1, var2, temp;
cout << "Enter value for first integer: ";
cin >> var1;
cout << "Enter value for second integer: ";
cin >> var2;
cout << "\nValues Before swapping" << endl;
cout << "First Integer: " << var1 << endl;
cout << "Second Interger: " << var2 << endl;
temp = var1;
var1 = var2;
var2 = temp;
cout << "\nValues After swapping" << endl;
cout << "First Integer: " << var1 << endl;
cout << "Second Interger: " << var2 << endl;
return 0;
}
Output:
Other Related Search Terms
- C Program to swap 2 variables using third variable
- C++ program to swap 2 variables using temp variable
- C and C++ program for swapping 2 variables using third variable

![[C/C++] Program to Swap 2 Variable Using Third Variable](https://fsconline.info/wp-content/uploads/2015/09/c-cpp-programming.jpg)
