Cho giá niêm yết (M) và giá bán (S). Tính phần trăm giảm giá.
Công thức: Giảm giá = M - S. Phần trăm giảm giá = (Giảm giá / M) * 100.
Ví dụ: M=240, S=180 → Giảm giá = 60, Phần trăm = 25%.
Giải thuật
- Đọc M, S.
- Tính discount = M - S.
- Tính discount_percent = (discount * 100) / M.
- In kết quả.
Ví dụ mã C++
#include <iostream>
using namespace std;
int main() {
float MP = 130, SP = 120;
float DP = (float)(((MP - SP) * 100) / MP);
printf("The discount percentage on the given product is %.2f\n", DP);
return 0;
}
Đầu vào
240, 180
Đầu ra
25%