Exercise 3.3 Calculate a discounted price

Exercise 3-3. Write a program that will calculate the price for a quantity entered from
the keyboard, given that the unit price is $5 and there is a discount of 10 percent for
quantities over 30 and a 15 percent discount for quantities over 50.

 1 // Exercise 3.3 Calculate a discounted price
 2 
 3 // I interpreted this exercise as implying that the 10% applies to items 31 to 50
 4 // and the 15% applies to items in excess of 50.
 5 // That is, you don't get 15% discount on the whole price when you order 51 items.
 6 
 7 // There is more than one way of doing this so different is not necessarily wrong.
 8 
 9 #include <stdio.h>
10 
11 int main(void)
12 {
13   const int level1 = 30;               // Quantity over this level are at discount1
14   const int level2 = 50;               // Quantity over this level are at discount2
15   const double discount1 = 0.10;       // 10% discount
16   const double discount2 = 0.15;       // 15% discount
17   const double unit_price = 5.0;       // Basic unit price
18   int quantity = 0;
19   int qty_full_price = 0;              // 0 to 30 at full price
20   int qty_level1 = 0;                  // 31 to 50 at level1 price
21   int qty_level2 = 0;                  // Over 50 at level2 price
22   printf("Enter the quantity that you require: ");
23   scanf("%d", &quantity);
24  
25   if(quantity > 50)                     // Quantity over 50
26   {
27     qty_full_price = level1;
28     qty_level1 = level2 - level1;
29     qty_level2 = quantity - level2;
30   }
31   else if(quantity > 30)                // Quantity is from 30 to 50
32   {
33     qty_full_price = level1;
34     qty_level1 = quantity - level1;
35   }
36   else
37     qty_full_price = quantity;
38 
39   printf("The total price for %d items is $%.2lf\n", quantity,
40     unit_price*(qty_full_price + (1.0 - discount1)*qty_level1 + (1.0 - discount2)*qty_level2));
41   return 0;
42 }

 

转载于:https://www.cnblogs.com/xiaomi5320/p/4170586.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值