C++程式計算利潤或虧損


給定成本價 (CP) 和售價 (SP),任務是計算獲得的利潤或發生的虧損。

成本價或 CP 是賣家購買產品的價格,售價或 SP 是賣家出售產品的價格。

有一個公式可以計算獲得的利潤或發生的虧損

利潤 = 售價 – 成本價

如果售價大於成本價,則會獲得利潤

虧損 = 成本價 – 售價

如果成本價大於售價,則會發生虧損

示例

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

給定程式中使用的步驟如下:

  • 將成本價和售價作為輸入
  • 應用給定的公式計算利潤或虧損
  • 顯示結果

演算法

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

示例

 即時演示

#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;
}

輸出

如果我們執行以上程式碼,它將生成以下輸出

loss incurred = 500

更新於: 2019年10月18日

3K+ 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始學習
廣告

© . All rights reserved.