C++ program to read two integers as input and swap them using call by reference, without using a third variable.
Answers
Answered by
6
Answer:
#include <iostream>
using namespace std;
int main()
{
int a=5, b=10;
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
a=a*b; //a=50 (5*10)
b=a/b; //b=5 (50/10)
a=a/b; //a=10 (50/5)
cout<<"After swap a= "<<a<<" b= "<<b<<endl;
return 0;
}
PLS MARK THE BRAILIEST PLS PLS PLS ......................
Answered by
0
C++ program to read two integers as input and swap them using call by reference, without using a third variable.
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
- In the above program two variables are assigned some value so that they can be swapped.
- After that some operations are used to swap the numbers and hence the output of the program will be the swapped numbers.
Similar questions
Science,
5 months ago
Social Sciences,
5 months ago
India Languages,
5 months ago
India Languages,
10 months ago
English,
10 months ago
English,
1 year ago
Science,
1 year ago