Introduction
Control Statement
- 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
- Program to find the sum of Natural Numbers using Recursion
Function
- 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
- Program to find LCM
Array
Pointer
OOPS
Program to swap two numbers using pointer
In this example, You will learn to swap two numbers using pointer. Here, We will use 5 variables. 3 of them are integer variables and 2 of them are pointer variables.
Before going through this example, You must have knowledge in following topics
Here, First we will take 2 input from user and stored in integer variables.
We will assign address of that variables to the pointer variables as like
a = &x;
b = &y;
Then we will assign pointer of a variable to temp variable.
temp = *a;
Again, We will assign pointer variable of b to pointer variable of a.
*a = *b
Finally, We will assign value of temp variable to pointer variable of a.
*b = temp;
On this way, Our program works and two numbers are swap using pointers.
Lets look an code for detail explanation.
Code: swap two numbers using pointer
#include<iostream>
using namespace std;
int main() {
int x, y, temp;
int *a, *b;
cout << "Enter two numbers:";
cin >> x>>y;
a = &x;
b = &y;
temp = *a;
*a = *b;
*b = temp;
cout << "After swap x is:" << x;
cout << "\nAfter swap y is:" << y;
return 0;
}
The output of above code is