From d6d21ef386aff8a99f9b4898d77cdb92e13c5676 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Sat, 17 Oct 2015 04:21:18 +0200 Subject: [PATCH] Done with exercise 5 in chapter 2 --- vagen_till_c/ch2/exercise5a.c | 31 +++++++++++++++++++++++++++++++ vagen_till_c/ch2/exercise5b.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 vagen_till_c/ch2/exercise5a.c create mode 100644 vagen_till_c/ch2/exercise5b.c diff --git a/vagen_till_c/ch2/exercise5a.c b/vagen_till_c/ch2/exercise5a.c new file mode 100644 index 0000000..7442381 --- /dev/null +++ b/vagen_till_c/ch2/exercise5a.c @@ -0,0 +1,31 @@ +#include + +/* Write a program that gives a customer 10% off the total price when he shops + * for at least 1000 SEK. Assume for simplicity that he only shops one kind of + * product. + */ + +int main() +{ + float discount = 10; + int limit = 1000; + int items; + float subtotal, total, price; + + printf("Number of items: "); scanf("%d", &items); + printf("Price per unit: "); scanf("%f", &price); + + subtotal = price * items; + + if (subtotal >= limit) + { + total = subtotal*((100-discount)/100); + } + else + { + total = subtotal; + } + + printf("Total price: %.2f\n", total); + return 0; +} diff --git a/vagen_till_c/ch2/exercise5b.c b/vagen_till_c/ch2/exercise5b.c new file mode 100644 index 0000000..3979f26 --- /dev/null +++ b/vagen_till_c/ch2/exercise5b.c @@ -0,0 +1,31 @@ +#include +#define DISCOUNT 10.0 +#define LIMIT 1000 + +/* Write a program that gives a customer 10% off the total price when he shops + * for at least 1000 SEK. Assume for simplicity that he only shops one kind of + * product. + */ + +int main() +{ + int items; + float subtotal, total, price; + + printf("Number of items: "); scanf("%d", &items); + printf("Price per unit: "); scanf("%f", &price); + + subtotal = price * items; + + if (subtotal >= LIMIT) + { + total = subtotal*((100-DISCOUNT)/100); + } + else + { + total = subtotal; + } + + printf("Total price: %.2f\n", total); + return 0; +}