C++ program to calculate Profit Or Loss


Given with the cost price(CP) and the selling price(SP) and the task is to calculate the profit gained or loss incurred.

Cost price or CP is the price at which the product is purchased by the seller and the selling price or SP is the price at which the product is sold by the seller.

There is a formula to calculate profit gained or loss incurred

Profit = Selling price – Cost price

If Selling price is greater than cost price than there will be a profit

Loss = Cost price – Selling price

If cost price is greater than selling price than there will be a loss

Example

Input-: CP = 600
   SP = 100
Output-: loss incurred = 500
Input-: CP = 100
   SP = 500
Output-: profit gained = 400

Approach used in the given program is as follows −

  • Take the input as cost price and selling price
  • Apply the formula given to calculate profit or loss
  • Display the result

Algorithm

Start
Step 1-> declare function to calculate Profit.
   int profit(int CP, int SP)
      set int profit = (SP - CP)
      return profit
step 2-> Declare function to calculate Loss
   int loss(int CP, int SP)
      set int loss = (CP - SP)
      return loss
step 3-> In main()
   set int CP = 600, SP = 100
      IF (SP == CP)
         Print "No profit nor Loss"
      End
      Else IF (SP > CP)
         call profit(CP, SP)
      End
      Else
         Call loss(CP , SP)
      End
Stop

Example

 Live Demo

#include <iostream>
using namespace std;
// Function to calculate Profit.
int profit(int CP, int SP) {
   int profit = (SP - CP);
   return profit;
}
// Function to calculate Loss.
int loss(int CP, int SP) {
   int loss = (CP - SP);
   return loss;
}
int main() {
   int CP = 600, SP = 100;
   if (SP == CP)
      cout << "No profit nor Loss";
   else if (SP > CP)
      cout<<"profit gained = "<< profit(CP, SP);
   else
      cout<<"loss incurred = "<<loss(CP , SP);
   return 0;
}

Output

IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT

loss incurred = 500

Updated on: 18-Oct-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements