编写一个 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