Write a C++ program to demonstrates call by pointer functionality in user defined function.

Program Code:
#include<iostream>
using namespace std;
void swap(int *, int *);
int main()
{
int a, b;
cout<<"Enter any two number to swap with each other : ";
cin>>a;
cin>>b;
cout<<endl;
cout<<"Value of entered number before swapping : "<<endl;

cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
swap(&a, &b);
cout<<"Value of entered number after swapping :"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
cout<<"Value of entered number within swapping function :"<<endl;
cout<<"a = "<<*x<<endl;
cout<<"b= "<<*y<<endl;
}

The program code is tested on www.jdoodle.com
Output:
Enter any two number to swap with each other : 10 15
Value of entered number before swapping : 
a = 10
b = 15
Value of entered number within swapping function :
a = 15
b= 10

Value of entered number after swapping :
a = 15
b = 10


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: