From 614d8aa3c8ceffea15763e57fcaa914dda1993a4 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Fri, 16 Oct 2015 03:33:02 +0200 Subject: [PATCH] Added another solution to the salary problem --- vagen_till_c/ch2/exercise4_ver2.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 vagen_till_c/ch2/exercise4_ver2.c diff --git a/vagen_till_c/ch2/exercise4_ver2.c b/vagen_till_c/ch2/exercise4_ver2.c new file mode 100644 index 0000000..ee9bed8 --- /dev/null +++ b/vagen_till_c/ch2/exercise4_ver2.c @@ -0,0 +1,23 @@ +#include +#define GOAL 100*1000000 +/* My solution to exercise 4, chapter 2 from "Vägen till C". + * A man is offered a highly dangerous job, with an unusual salary. + * First day he'll get 1 cent, second day 2 cents, third day 4 cents and so on. + * Write a program to calculate how many days he'll have to work to reach one + * million dollar. + */ + +int main() +{ + int salary = 1; + int days = 1; + int tot = 0; + + while(tot<=GOAL) + { + salary = salary * 2; + tot += salary; + days++; + } + printf("He'll have to work %d days to reach %d dollar\n", days, GOAL/100); +}