กำหนดด้วยราคาต้นทุน (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