C++ Example - Swap Two Numbers |
-- Learning not just technology, but dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
C++ Tutorial
C++ Tutorial C++ Introduction C++ Environment Setup C++ Basic Syntax C++ Comments C++ Data Types C++ Variable Types C++ Variable Scope C++ Constants C++ Modifier Types C++ Storage Classes C++ Operators C++ Loops C++ Decision Making C++ Functions C++ Numbers C++ Arrays C++ Strings C++ Pointers C++ References C++ Date & Time C++ Basic Input/Output C++ Struct C++ Vector Container C++ Data Structures
C++ Object-Oriented
C++ Classes & Objects C++ Inheritance C++ Overloading C++ Polymorphism C++ Data Abstraction C++ Data Encapsulation C++ Interfaces (Abstract Classes)
C++ Advanced Tutorial
C++ Files and Streams C++ Exception Handling C++ Dynamic Memory C++ Namespaces C++ Templates C++ Preprocessor C++ Signal Handling C++ Multithreading C++ Web Programming
C++ Resources
C++ STL Tutorial C++ Import Standard Library C++ Standard Library C++ Useful Resources C++ Examples C++ Quiz <a href="#" title="C++ Standard Input/Output - ">C++ Standard Input/Output - <iostream> <a href="#" title="C++ File I/O Library - ">C++ File I/O Library - <fstream> <a href="#" title="C++ Standard Library ">C++ Standard Library <sstream> <a href="#" title="C++ Standard Library ">C++ Standard Library <iomanip> <a href="#" title="C++ Container Class ">C++ Container Class <array> <a href="#" title="C++ Container Class ">C++ Container Class <vector> <a href="#" title="C++ Container Class ">C++ Container Class <list> <a href="#" title="C++ Container ">C++ Container <forward_list> <a href="#" title="C++ Container Class ">C++ Container Class <deque> <a href="#" title="C++ Container Class ">C++ Container Class <stack> <a href="#" title="C++ Container Class ">C++ Container Class <queue> <a href="#" title="C++ Container Class ">C++ Container Class <priority_queue> <a href="#" title="C++ Container Class ">C++ Container Class <set> <a href="#" title="C++ Container Class ">C++ Container Class <unordered_set> <a href="#" title="C++ Container Class
C++ Example - Swap Two Numbers
Below we use two methods to swap two variables: using a temporary variable and without using a temporary variable.
Example - Using a Temporary Variable
#include<iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping:" << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "nAfter swapping:" << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
The output of the above program execution is:
Before swapping:
a = 5, b = 10
After swapping:
a = 10, b = 5
Example - Without Using a Temporary Variable
#include<iostream>
using namespace std;
int main()
{
int a = 5, b = 10;
cout << "Before swapping:" << endl;
cout << "a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "nAfter swapping:" << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
The output of the above program execution is:
Before swapping:
a = 5, b = 10
After swapping:
a = 10, b = 5
YouTip