Profit and loss Problems using C

CServer Side ProgrammingProgramming

Given a certain cost price(cp) and a selling price(sp) of an unknown product or a service, our task is to find the profit earned or loss suffered using a C program where if the profit is earned should print “Profit” and it’s amount or if the loss is suffered “Loss” and its respective amount or if there is not profit no loss then print “No profit nor Loss”.

To find the profit or the loss we generally see whether the selling price(sp) or the price/amount at which a certain thing is sold or the cost price(cp) at which a certain thing is bought. If the cost price(cp) is higher than the selling price(sp) then it is believed that there is loss, and their difference will be the total loss suffered. When the selling price(sp) is higher than the cost price(cp) then it is believed that profit is earned, and their difference is the total profit.

Input − cp = 149, sp = 229

Output − Profit 80

Explanation −selling price(sp) > cost price(cp), so there is profit of sp-cp=80

Input − cp = 149, sp = 129

Output −Loss 20

Explanation −cost price(cp) > selling price(sp), so there is loss of cp-sp=20

Approach used below is as follows to solve the problem

  • Take the cost price and selling price as input

  • Check if cost price > selling price, then it is a loss find difference and return the result.

  • Check if selling price > cost price, then it is a profit find difference and return the result.

  • And if selling price == cost price, then there is not profit nor a loss.

Algorithm

Start
In function int Profit(int cp, int sp)
   Step 1→ Return (sp - cp)
In function int Loss(int cp, int sp)
   Step 1→ Return (cp - sp)
In function int main()
   Step 1→ Declare and initialize cp = 5000, sp = 6700
   Step 2→ If sp == cp then,
      Print "No profit nor Loss"
   Step 3→ Else if sp > cp
      Print Profit
   Step 4→ Else
      Print Loss
Stop

Example

 Live Demo

#include <stdio.h>
//Function will return profit
int Profit(int cp, int sp){
   return (sp - cp);
}
// Function will return Loss.
int Loss(int cp, int sp){
   return (cp - sp);
}
int main(){
   int cp = 5000, sp = 6700;
   if (sp == cp)
      printf("No profit nor Loss\n");
   else if (sp > cp)
      printf("%d Profit\n", Profit(cp, sp));
   else
      printf("%d Loss\n", Loss(cp, sp));
   return 0;
}

Output

If run the above code it will generate the following output −

1700 Profit
raja
Published on 13-Aug-2020 08:28:57
Advertisements