86.8k views
4 votes
You have two boxes (bl and b2). bl contain 10 apples, b2 contain 5 oranges. Write a program in C++ to swap bl contents with b2 contents.

User Unixsmurf
by
7.5k points

1 Answer

7 votes

Final answer:

In C++, a simple program can swap the contents of two variables representing boxes containing apples and oranges by using a temporary variable to hold one of the values during the swap.

Step-by-step explanation:

C++ Program to Swap Box Contents

To swap the contents of two boxes in C++, we can define a simple program using a temporary variable. Below is a sample code snippet that demonstrates how to do this:

#include
using namespace std;

int main() {
int b1 = 10; // Box1 has 10 apples
int b2 = 5; // Box2 has 5 oranges
int temp;

// Swapping using a temporary variable
temp = b1;
b1 = b2;
b2 = temp;

cout << "After swapping, b1 has " << b1 << " oranges and b2 has " << b2 << " apples." << endl;

return 0;
}

This code will swap the contents of the two boxes b1 and b2 and display the updated contents.

User Roganjosh
by
7.8k points