C++ Program to Swap Two Numbers Using Temp Variable

C++ Program to swap two numbers using temporary variable. This program will change the numbers position using the c++. 

Swapping the numbers means it will interchange the number which are allocated in the c++ programming language.

#include <iostream>
using namespace std;
int main()
{
int a = 50, b = 10, temp,c;
cout << "Before" << c;
cout << "a = " << a << ", b = " << b << c;
temp = a;
a = b;
b = temp;
cout << "\nAfter" << c;
cout << "a = " << a << ", b = " << b << c;
return 0;
}

 

Output:

Before a = 50, b = 10

After a = 10, b = 50

Temporary Variable in C++

A temporary variable is a variable in computer programming that has a limited lifetime and is used to retain data that will be deleted shortly or before it can be stored in a more permanent memory location. 

It’s generally stated as a local variable, or a variable with a local scope because it’s short-lived.

Namespace in C++

A return statement terminates a function’s execution and returns control to the calling function. At the point immediately following the call, execution resumes in the calling function. 

A return statement can return a value to the function that called it. The execution of a function is terminated, and control is returned to the calling function. 

At the point immediately following the call, execution resumes in the calling function.