Introduction
Control Statement
- Check Whether the Number is Even or Odd in C++
- Program to check whether the number is prime or not
- Program to check vowel or constant
- Program to check the largest number among three numbers
- Program to the sum of natural numbers
- Program to check leap year
- Program to find factorial of the number
- Program to find reverse of the number
- Program to Check Armstrong Number
Function
- Program to find LCM
- Program to check prime number using Functions
- Program to convert Binary to Decimal Number
- Program to convert Decimal to Binary Number
- Program to convert Binary to Octal Number
- Program to convert Octal to Binary Number
- Program to find reverse of the sentence using Recursion
- Program to Shutdown and restart Computer
- Program to find the area of triangle
Array
Pointer
OOPS
No Examples found for this topic - CodeHelpPro
Program to swap two number
In this article, we will learn how to swap two numbers using the C++ programming language.
We will do this using two technique.
- Method 1: Using Temporary variable
- Method 2: Without using Temporary variable
Example 1: Swap Two using Temporary variables
In this example, We will use the temporary variable to swap two numbers. We will three variables to swap numbers.
We will do this using following steps:
- Step 1: Assign value of first variable to temporary variable
- Step 2: Assign second variable value to first variable
- Step 3: Assign Temporary variable value to second variable
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20, 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 above program is
Before swapping :
a = 10, b = 20
After swapping :
a = 20, b = 10
Example 2 : Swap two number without temporary variable
In this example, We will swap two numbers without using the temporary variable.
We will do this using following steps:
- Step 1: Initially Declare variables and assign values
- Step 2: Add two variables and assign to first variable
- Step 3: We will subtract second variable from first variable and assign to second variable
- Step 4: Subtract second variable from first variable and assign to first variable
Let look on visual view
a = 10, b = 20
a = a + b // value = 30
b = a - b // Value = 30 - 20 = 10
a = a - b // Value = 30 - 10 = 20
//output
a = 20 and b = 10
We cannot use multiplication and division here instead of addition. Let’s look on full code to make more clear on this program.
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20;
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 this program is same as above program.
Subscribe
Login
0 Comments
Inline Feedbacks
View all comments