Use the Online C++ compiler: https://www.onlinegdb.com/online c++ compiler Delete all the existing code from main.cpp. You will write the code in the file main.cpp. Write a C++ program to input cost price (int) and sell price (int) of an item. Display the correct message as shown in the sample output, if the seller makes a profit, a loss, or no profit or loss. When you are done: Download your code. It will save the file main.cpp and rename the file to Lab3_Ex2.cpp. This is very important so that you can always access the code later. How to Submit: Select all the code using Ctrl + A / Cmd + A. Copy the code using Ctrl + C / Cmd + C. Paste the code in the answer box using Ctrl + V / Cmd + V on the blackboard. Ignore if the formatting of the code is different. Move to the next question or press the Save and Submit button. Sample Input / Output: Enter cost price: 100 Enter sell price: 120 You made a profit of 20 dirhams Enter cost price: 200 Enter sell price: 100 You made a loss of 100 dirhams Enter cost price: 60 Enter sell price: 60 No profit, no loss.

The Correct Answer and Explanation is:

Here’s the C++ code you should enter into main.cpp using the OnlineGDB compiler:

cpp

#include <iostream>
using namespace std;

int main() {
    int costPrice, sellPrice;
    
    cout << "Enter cost price: ";
    cin >> costPrice;
    
    cout << "Enter sell price: ";
    cin >> sellPrice;
    
    if (sellPrice > costPrice) {
        int profit = sellPrice - costPrice;
        cout << "You made a profit of " << profit << " dirhams" << endl;
    } else if (sellPrice < costPrice) {
        int loss = costPrice - sellPrice;
        cout << "You made a loss of " << loss << " dirhams" << endl;
    } else {
        cout << "No profit, no loss" << endl;
    }
    
    return 0;
}

Explanation

This C++ program helps a seller determine if they have made a profit, a loss, or neither after selling an item. It begins by including the iostream header file, which allows the program to perform input and output operations. The main() function is where the program’s execution begins.

The user is prompted to enter two integers: the cost price and the sell price. The program stores these inputs in variables named costPrice and sellPrice. A conditional structure (if, else if, and else) is used to compare the two prices.

If the sell price is greater than the cost price, the seller has made a profit. The profit is calculated by subtracting the cost from the selling price, and the result is displayed as part of a descriptive sentence. If the cost price is greater than the sell price, the seller has made a loss, and the program calculates and displays the loss amount similarly. If the cost price and sell price are equal, the program states that there is no profit and no loss.

This solution is concise and uses only fundamental constructs of C++, including input/output operations and conditional statements. It does not use advanced data types or structures, making it ideal for beginners learning decision-making in programming.

Make sure to delete the existing code in main.cpp, paste this new code, and run it using OnlineGDB. After confirming it works, download the file and rename it to Lab3_Ex2.cpp as instructed. Once ready, copy and paste the code into your submission portal.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *