C++ Program to Calculate Profit or Loss

In this post, we will learn how to calculate profit or loss using C++ Programming language.

  • If selling price > cost price, then the difference between the selling price and the cost price is the profit.
  • If cost price > selling price, then the difference between the cost price and the selling price is the loss.
  • And if selling price == cost price, there is neither profit nor loss.

We will be using the same approach to calculate profit or loss using for loop statement.

So, without further ado, let’s begin this tutorial.

C++ Program to Calculate Profit or Loss

// C++ Program to Calculate Profit OR Loss
#include <iostream>
using namespace std;

int main(){
    int cp, sp, profit, loss;
    
    // Asking for input
    cout << "Enter the cost price: ";
    cin >> cp;
    cout << "Enter the selling price: ";
    cin >> sp;
    
    // Checking profit or loss
    if (sp > cp){
        profit = sp - cp;
        cout << "Profit Gained: " << profit;
    }
    else if (cp > sp){
        loss = cp - sp;
        cout << "Loss Incurred: " << loss;
    }
    else{
        cout << "No profit nor loss" << endl;
    }
    return 0;
}

Output 1 

Enter the cost price: 700
Enter the selling price: 900
Profit Gained: 200

Output 2

Enter the cost price: 450
Enter the selling price: 400
Loss Incurred: 50

Output 3

Enter the cost price: 125
Enter the selling price: 125
No profit nor loss

How Does This Program Work ?

    int cp, sp, profit, loss;

In this program, we have declared four int data type variables named cp, sp, profit and loss.

    // Asking for input
    cout << "Enter the cost price: ";
    cin >> cp;
    cout << "Enter the selling price: ";
    cin >> sp;

Then, the user is asked to enter the cost price and selling price of the product.

    // Checking profit or loss
    if (sp > cp){
        profit = sp - cp;
        cout << "Profit Gained: " << profit;
    }

If sp > cp, then there is a profit of sp – cp amount.

    else if (cp > sp){
        loss = cp - sp;
        cout << "Loss Incurred: " << loss;
    }

If cp > sp, then there is a loss of cp – sp amount.

    else{
        cout << "No profit nor loss" << endl;
    }

And if neither of the two statements are true, that means sp = cp. In this case there is neither profit nor loss.

Conclusion

I hope after going through this post, you understand how to calculate profit or loss using C++ Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published.