Swapping of two numbers in C++
In this post, you will learn how to swap two numbers using C++ programming language.
Here, we have mentioned three ways to swap two numbers. The first program uses a temporary variable, while the others does not use any temporary variable. The swapping numbers program is frequently asked in C++ coding tests and interviews.
C++ swap two numbers using temporary variable
In the given C++ program, we use a temporary variable, 'temp' to swap two numbers. First, we assign the value of 'x' to 'temp'. Then, assign the value of 'y' to 'x' and the value of 'temp' to 'y'. Finally, the value of 'x' will be assigned to 'y' and the value of 'y' will be assigned to 'x'.
#include <iostream>
using namespace std;
int main()
{
int x = 100, y = 150, temp;
cout << "Before swapping:" << endl;
cout << "x = " << x << ", y = " << y << endl;
temp = x;
x = y;
y = temp;
cout << "\nAfter swapping:" << endl;
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
Output of the above code:
Before swapping:
x = 100, y = 150
After swapping:
x = 150, y = 100
C++ swap two numbers without using temporary variable
In the given C++ program, we ask the user to enter two integer values and store them in two variables. Then, we swap the entered value using the addition and subtraction operators.
#include <iostream>
using namespace std;
int main()
{
int x = 220, y = 110;
cout << "Before swapping:" << endl;
cout << "x = " << x << ", y = " << y << endl;
x = x + y;
y = x - y;
x = x - y;
cout << "\nAfter swapping:" << endl;
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
Output of the above code:
Before swapping:
x = 220, y = 110
After swapping:
x = 110, y = 220
C++ swap two numbers using Multiplication and division
In the given C++ program, we swap two numbers using the arithmetic operators multiplication and division.
#include <iostream>
using namespace std;
int main()
{
int x = 320, y = 220;
cout << "Before swapping:" << endl;
cout << "x = " << x << ", y = " << y << endl;
x = x * y;
y = x / y;
x = x / y;
cout << "\nAfter swapping:" << endl;
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
Output of the above code:
Before swapping:
x = 320, y = 220
After swapping:
x = 220, y = 320
C++ program to swap two numbers using Bitwise XOR
Here, we have used the bitwise XOR operator to swap two variables using C++. The XOR of two numbers x and y returns a number that has all the bits as 1 wherever bits of x and y differ.
#include <iostream>
using namespace std;
int main()
{
int x = 70, y = 55;
cout << "Before Swapping: x=" << x << ", y=" << y;
// Code to swap 'x' and 'y'
x = x ^ y;
y = x ^ y;
x = x ^ y;
cout << "\nAfter Swapping: x=" << x << ", y=" << y;
return 0;
}
Output of the above code:
Before Swapping: x=70, y=55
After Swapping: x=55, y=70
Related Articles
Implementation of queue using array in CQueue implementation in Python
Queue Implementation in C
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to convert Decimal to Octal
C program to convert decimal to binary
C program to check whether a number is even or odd