編寫一個 C 程式來算出購買一件商品的利潤或虧損


如果售價大於成本價,則求利潤的公式如下 -

profit=sellingPrice-CosePrice;

如果成本價大於售價,則求虧損的公式如下 -

loss=CostPrice-SellingPrice

現在,在程式中應用此邏輯,並嘗試查詢某人購買任何物品後是否獲利或虧損 -

例如

以下是用於查詢利潤或虧損的 C 程式 -

 即時演示

#include<stdio.h>
int main(){
   float CostPrice, SellingPrice, Amount;
   printf("
Enter the product Cost : ");    scanf("%f", &CostPrice);    printf("
Enter the Selling Price) : ");    scanf("%f", &SellingPrice);    if (SellingPrice > CostPrice){       Amount = SellingPrice - CostPrice;       printf("
Profit Amount = %.4f", Amount);    }    else if(CostPrice> SellingPrice){       Amount = CostPrice - SellingPrice;       printf("
Loss Amount = %.4f", Amount);    }    else       printf("
No Profit No Loss!");    return 0; }

輸出

當執行上述程式時,將產生以下結果 -

Enter the Product Cost: 450
Enter the Selling Price): 475.8
Profit Amount = 25.8000

更新於:08-Mar-2021

258 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.