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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP