Answer to Question #260588 in C++ for mannya

Question #260588

Illustrate the use of pointers in swapping two numbers.

Write 2 swap functions, for swapping two integers.

void swap(int,int);

void swap(int*,int*);


1
Expert's answer
2021-11-03T08:46:55-0400
#include <iostream>
using namespace std;

void swap(int*,int*);
int main(){
	int num1, num2;
	cout<<"Swap two numbers\n";
	cout<<"Enter first number: \n";
	cin>>num1;
	cout<<"Enter second number: \n";
	cin>>num2;
	
	cout<<"Before swapping \n"<<"first number= "<<num1<<" Second number: "<<num2;
	
	swap(&num1,&num2);
	
	cout<<"\nAfter swapping \n"<<"first number= "<<num1<<" Second number: "<<num2;
	return 0;
}
void swap(int*a, int*b){
	int temp;
	temp=*a;
	*a=*b;
	*b=temp;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS