C++ program to swap values of two variables by passing pointers


C++ program to swap values of two variables by passing pointers


IDE used: - Code::Blocks

Image sample of program: -


C++ program to swap values of two variables by passing pointers
techee India

Program: -


#include<iostream>
using namespace std;
int main()
{
    void swap(int*x,int*y);
    int a,b;
    cout<<"\nEnter the value of A ";
    cin>>a;
    cout<<"\nEnter the value of B ";
    cin>>b;
    cout<<"\nOriginal values "<<"A="<<a<<",B="<<b<<"\n";
    swap(&a,&b);
    cout<<"\nSwapped values "<<"A="<<a<<",B="<<b<<"\n";
    return 0;
}
void swap(int*x,int*y)
{
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;
}



Output of the program: -

Here we enter initial value of A=5 and B=7
So,desired output will be A=7 and B=5 after swapping.

Image sample of output: - 


C++ program to swap values of two variables by passing pointers
techee INDIA


Hope you like the C++ program to swap values of two variables by passing pointers.


JAI HIND



C++ program to swap values of two variables by passing pointers

C++ program to swap values of two variables by passing pointers C++ program to swap values of two variables by passing pointers Reviewed by mushrafkhan772 on September 19, 2018 Rating: 5

No comments:

Powered by Blogger.