คำอธิบายปัญหา
ให้ราคาตั้งขาย (Marked Price = M) และราคาขาย (Selling Price = S) หาเปอร์เซ็นต์ส่วนลด
สูตร
- ส่วนลด (Discount) = M - S
- เปอร์เซ็นต์ส่วนลด = (Discount / M) × 100
ตัวอย่าง
M = 240, S = 180
Discount = 60
Discount % = (60/240) × 100 = 25%
ตัวอย่างโค้ด C++
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
float MP = 240, SP = 180;
float discount = MP - SP;
float discountPercent = (discount / MP) * 100;
printf("Discount = %.2f\n", discount);
printf("Discount Percentage = %.2f%%\n", discountPercent);
return 0;
}
ผลลัพธ์
Discount = 60.00 Discount Percentage = 25.00%
คำอธิบายเพิ่มเติม
- ใช้ float/double เพื่อความแม่นยำของทศนิยม
- printf กับ format specifier %.2f แสดง 2 ตำแหน่งทศนิยม
- %% ใน printf ใช้พิมพ์เครื่องหมาย %