188k views
5 votes
Write a C++ program to calculate profit or loss using a switch statement.

1 Answer

1 vote

Final answer:

The student's question is about creating a C++ program that calculates profit or loss using a switch statement. The example code provided takes input for cost price and selling price, computes the difference, and uses a switch statement to determine if there was a profit or a loss.

Step-by-step explanation:

The question asks for a C++ program that uses a switch statement to calculate profit or loss. Here's a simple example of such a program:

#include
using namespace std;

int main() {
double costPrice, sellingPrice, result;
cout << "Enter cost price: ";
cin >> costPrice;
cout << "Enter selling price: ";
cin >> sellingPrice;

result = sellingPrice - costPrice;

switch(result > 0) {
case true:
cout << "Profit = $" << result;
break;
case false:
// Check if there was no profit, no loss
if(result == 0) {
cout << "No profit, no loss.";
} else {
cout << "Loss = $" << -result;
}
break;
}
return 0;
}

The program takes the cost price and selling price as input and calculates the difference. If the result is positive, there's a profit; if it's negative, there's a loss. The switch statement is used here for demonstration purposes, though traditionally this could also be served by an if-else construct.

User VKarthik
by
7.4k points