YouTip LogoYouTip

Cpp Examples Swapping

C++ Example - Swap Two Numbers |

-- Learning not just technology, but dreams!

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++ Container Class <map> <a href="#" title="C++ Container Class ">C++ Container Class <unordered_map> <a href="#" title="C++ Container Class ">C++ Container Class <bitset> <a href="#" title="C++ Algorithm Library ">C++ Algorithm Library <algorithm> <a href="#" title="C++ Standard Library ">C++ Standard Library <iterator> <a href="#" title="C++ Standard Library ">C++ Standard Library <functional> <a href="#" title="C++ Standard Library ">C++ Standard Library <numeric> <a href="#" title="C++ Standard Library ">C++ Standard Library <complex> <a href="#" title="C++ Standard Library ">C++ Standard Library <valarray> <a href="#" title="C++ Standard Library ">C++ Standard Library <cmath> <a href="#" title="C++ Standard Library ">C++ Standard Library <string> <a href="#" title="C++ Standard Library ">C++ Standard Library <regex> <a href="#" title="C++ Standard Library ">C++ Standard Library <ctime> <a href="#" title="C++ Standard Library ">C++ Standard Library <chrono> <a href="#" title="C++ Multithreading Library ">C++ Multithreading Library <thread> <a href="#" title="C++ Standard Library ">C++ Standard Library <mutex> <a href="#" title="C++ Standard Library ">C++ Standard Library <condition_variable> <a href="#" title="C++ Standard Library ">C++ Standard Library <future> <a href="#" title="C++ Standard Library ">C++ Standard Library <atomic> <a href="#" title="C++ Standard Library ">C++ Standard Library <type_traits> <a href="#" title="C++ Standard Library ">C++ Standard Library <typeinfo> <a href="#" title="C++ Exception Handling Library ">C++ Exception Handling Library <exception> <a href="#" title="C++ Exception Handling Library ">C++ Exception Handling Library <stdexcept> <a href="#" title="C++ Standard Library ">C++ Standard Library <cstdio> <a href="#" title="C++ Standard Library ">C++ Standard Library <cstdint> <a href="#" title="C++ Memory Management Library ">C++ Memory Management Library <memory> <a href="#" title="C++ Memory Management Library ">C++ Memory Management Library <new> <a href="#" title="C++ Standard Library ">C++ Standard Library <utility> <a href="#" title="C++ Standard Library ">C++ Standard Library <random> <a href="#" title="C++ Standard Library ">C++ Standard Library <locale> <a href="#" title="C++ Standard Library ">C++ Standard Library <codecvt> <a href="#" title="C++ Standard Library ">C++ Standard Library <cassert> <a href="#" title="C++ Standard Library ">C++ Standard Library <cwchar> <a href="#" title="C++ Standard Library ">C++ Standard Library <climits> <a href="#" title="C++ Standard Library ">C++ Standard Library <cfloat> <a href="#" title="C++ Standard Library ">C++ Standard Library <cstdlib> <a href="#" title="C++ Standard Library ">C++ Standard Library <numbers> C++ OpenCV

C++ Useful Resources

C++ Quiz

C++ Example - Swap Two Numbers

C++ Examples C++ Examples

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

C++ Examples C++ Examples

C++ Useful Resources

← Cpp Examples Vowel ConsonantCpp Examples Quotient Remainde β†’