首页 > 代码库 > Exercise 3.3 Calculate a discounted price
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 discount114 const int level2 = 50; // Quantity over this level are at discount215 const double discount1 = 0.10; // 10% discount16 const double discount2 = 0.15; // 15% discount17 const double unit_price = 5.0; // Basic unit price18 int quantity = 0;19 int qty_full_price = 0; // 0 to 30 at full price20 int qty_level1 = 0; // 31 to 50 at level1 price21 int qty_level2 = 0; // Over 50 at level2 price22 printf("Enter the quantity that you require: ");23 scanf("%d", &quantity);24 25 if(quantity > 50) // Quantity over 5026 {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 5032 {33 qty_full_price = level1;34 qty_level1 = quantity - level1;35 }36 else37 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 }
Exercise 3.3 Calculate a discounted price
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。