Q:

C++ Program For Swapping Number Using Pointer

0
Explanation:-
 Swapping the two number is very easy we have to take a two number entered by user and pass the both two number in function by reference or pass by value here we are passing the both number by reference(passing address of variable) and in function we put the swapping condition in function as you can see in the below program. after the swapping the number either we can return the result to function or we can directly print the swapped numbers in function. In this swapping method (call by reference) anything changed in the value will also affect the actual parameters but here we print the swapped value in the same function But it is not necessary we can directly print the value in the main function cause we pass the value through call by reference not call by value.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<bits/stdc++.h>
using namespace std;

void swap(int&, int&);    

int main()
{
    int first, second;

 cout<<"Enter The Two Value \n";
    cin>>first>>second;
     
    cout << "\nBefore Swapping Values Are\n" << endl;
 
 cout<<"First Value is = "<<first<<endl;
 cout<<"Second Value is = "<<second<<endl;
 
    swap(first, second);
 
 return 0;
}

void swap(int &first, int &second)
{
    int temp;
    temp = first;
    first = second;
    second = temp;
    
    cout << "\n\nAfter Swapping Values Are\n" << endl;
 
 cout<<"First Value is = "<<first<<endl;
 cout<<"Second Value is = "<<second<<endl;
}

 

Output:

Enter The Two Value 

1000

2000

Before Swapping Values Are

First Value is = 1000

Second Value is = 2000

After Swapping Values Are

First Value is = 2000

Second Value is = 1000

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program To Print Address of An Array Using Poi... >>
<< C++ Program To Find Average of An Array Elements U...