編寫一個 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP