Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Tìm giá vốn từ giá bán và phần trăm lãi/lỗ trong C++

Cho giá bán (SP) và phần trăm lãi hoặc lỗ. Tính giá vốn (CP).

  • Có lãi: CP = (SP × 100) / (100 + %lãi)
  • Có lỗ: CP = (SP × 100) / (100 - %lỗ)

Ví dụ

#include <iostream>
using namespace std;

float priceWhenProfit(int sellPrice, int profit) {
    return (sellPrice * 100.0) / (100 + profit);
}

float priceWhenLoss(int sellPrice, int loss) {
    return (sellPrice * 100.0) / (100 - loss);
}

int main() {
    int SP = 1020, profit = 20;
    cout << "Cost Price When Profit: " << priceWhenProfit(SP, profit) << endl;
    SP = 900; int loss = 10;
    cout << "Cost Price When Loss: " << priceWhenLoss(SP, loss) << endl;
}

Kết quả

Cost Price When Profit: 850
Cost Price When Loss: 1000