Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

หาราคาขายจากเปอร์เซ็นต์กำไรและต้นทุนใน C++

พิจารณาว่ามีราคาขาย และเปอร์เซ็นต์กำไรหรือขาดทุนกำหนด เราต้องหาราคาทุนของสินค้า สูตรคือดังนี้:

$$Cost Price=\frac{Sell Price\times 100}{100+percentage\ profit}$$ $$Cost Price=\frac{Sell Price\times 100}{100-percentage\ loss}$$

ตัวอย่าง

#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, profit, loss;
    SP = 1020;
    profit = 20;
    cout << "Cost Price When Profit: " << priceWhenProfit(SP, profit) << endl;
    SP = 900;
    loss = 10;
    cout << "Cost Price When loss: " << priceWhenLoss(SP, loss) << endl;
}

ผลลัพธ์

Cost Price When Profit: 850
Cost Price When loss: 1000