C program to swap two numbers using pointers - C++ coding

Here is a simple C++ Program that show the use of pointers to Swap Two Numbers or Variables using Pointer. You need to enter the numbers and your number will be reversed or swapped. Lets see the program.

Here is a simple C++ Program that show the use of pointers to Swap Two Numbers or Variables using Pointer. You need to enter the numbers and your number will be reversed or swapped. Lets see the program.

c program to swap two numbers using pointers

Here is the program.

#include<iostream>
using namespace std;
int swap(int*num1,int*num2){
int temp;
temp=*num1;
*num1=*num2;*num2=temp;
return 0;}
int main(){
int num1,num2;
cout<<"nEnter the first number : ";
cin>>num1;
cout<<"nEnter the Second number : ";
cin>>num2;
swap(&num1,&num2);
cout<<"nAfter swappingnfirst no. is: "<<num1;
cout<<"nsecond no. is: "<<num2;
return(0);}

Here is the output

 Program to swap 2 numbers using pointers

Enter the first number : 89

Enter the Second number : 67

After swapping
first no. is: 67
second no. is: 89
--------------------------------

Explaination

Swapping Two Variables using Pointer Firstly we need to accept two numbers using cin statement this. Now You need to pass the address of both variables to the function we used & operator to give address of num1 and num2. swap(&num1,&num2); Parameter passing scheme is called as Pass by Pointer.
Now Take a look at function definition, We have passed address of two variables from main function to swap function. So we need variable container that can store the address of integer variables.
int swap(int*num1,int*num2);
Thus address of first number will be collected in “num1” pointer variable and second number will be collected in “num2” pointer variable.
temp  =*num1;*num1=*num2;*num2=temp; is used to swap number from one to other.

Name

ltr
item
MindxMaster: C program to swap two numbers using pointers - C++ coding
C program to swap two numbers using pointers - C++ coding
Here is a simple C++ Program that show the use of pointers to Swap Two Numbers or Variables using Pointer. You need to enter the numbers and your number will be reversed or swapped. Lets see the program.
https://3.bp.blogspot.com/-6B7UXDIknfI/Vr_s7dRQUbI/AAAAAAAAAIg/yOFnm8qONOg/s1600/swap.jpg
https://3.bp.blogspot.com/-6B7UXDIknfI/Vr_s7dRQUbI/AAAAAAAAAIg/yOFnm8qONOg/s72-c/swap.jpg
MindxMaster
http://www.mindxmaster.com/2016/02/simple-c-program-for-swap-numbers-or.html
http://www.mindxmaster.com/
http://www.mindxmaster.com/
http://www.mindxmaster.com/2016/02/simple-c-program-for-swap-numbers-or.html
true
5332415103371288268
UTF-8