สูตรการหากำไรจะเป็นดังนี้ถ้าราคาขายมากกว่าราคาต้นทุน -
profit=sellingPrice-CosePrice;
สูตรการหาการสูญเสียจะเป็นดังนี้ถ้าราคาต้นทุนมากกว่าราคาขาย -
loss=CostPrice-SellingPrice
ตอนนี้ ใช้ตรรกะนี้ในโปรแกรมและพยายามค้นหาว่าบุคคลนั้นได้รับผลกำไรหรือขาดทุนหลังจากซื้อบทความใดๆ หรือไม่ –
ตัวอย่าง
ต่อไปนี้เป็นโปรแกรม C เพื่อค้นหากำไรหรือขาดทุน -
#include<stdio.h> int main(){ float CostPrice, SellingPrice, Amount; printf("\n Enter the product Cost : "); scanf("%f", &CostPrice); printf("\n Enter the Selling Price) : "); scanf("%f", &SellingPrice); if (SellingPrice > CostPrice){ Amount = SellingPrice - CostPrice; printf("\n Profit Amount = %.4f", Amount); } else if(CostPrice> SellingPrice){ Amount = CostPrice - SellingPrice; printf("\n Loss Amount = %.4f", Amount); } else printf("\n No Profit No Loss!"); return 0; }
ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
Enter the Product Cost: 450 Enter the Selling Price): 475.8 Profit Amount = 25.8000