diff --git a/vagen_till_c/ch2/exercise2.c b/vagen_till_c/ch2/exercise2.c new file mode 100644 index 0000000..90008e6 --- /dev/null +++ b/vagen_till_c/ch2/exercise2.c @@ -0,0 +1,24 @@ +#include + +/* My solution to exercise 2, chapter 2 from "Vägen till C". + * Write a program to calculate how many km a car has been driven during a year + * a much fuel it consumes on avererage + */ + +int main() +{ + float km_today, km_lastyear, fuel; + + printf("Odometer last year: "); + scanf("%f", &km_lastyear); + printf("Odometer today: "); + scanf("%f", &km_today); + printf("Total fuel consumed during the year: "); + scanf("%f", &fuel); + + printf("\nTotal distance driven: %.1f\n", km_today-km_lastyear); + printf("Average fuel consumption: %.2f\n", fuel/(km_today-km_lastyear)); + + return 0; +} + diff --git a/vagen_till_c/ch2/exercise3.c b/vagen_till_c/ch2/exercise3.c new file mode 100644 index 0000000..0decb35 --- /dev/null +++ b/vagen_till_c/ch2/exercise3.c @@ -0,0 +1,27 @@ +#include + +/* My solution to exercise 3, chapter 2, from the book "Vägen till C". + * Write a program to produce a table with the square and cube up to a given + * number, starting from 1. + */ + +int main() +{ + int n, i, s; + s = 1; + + printf("Enter an integer: "); + scanf("%d", &n); + + printf(" i i*i i*i*i \n"); + printf("=== ===== =======\n"); + + for(i=0; i<=n; i++) + { + printf(" %1d %7d %8d \n", s, s*s, s*s*s); + s++; + } + + return 0; +} + diff --git a/vagen_till_c/ch2/exercise4.c b/vagen_till_c/ch2/exercise4.c new file mode 100644 index 0000000..f2a9b15 --- /dev/null +++ b/vagen_till_c/ch2/exercise4.c @@ -0,0 +1,21 @@ +#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; + + while(salary<=GOAL) + { + salary = salary * 2; + days++; + } + printf("He'll have to work %d days to reach %d dollar\n", days, GOAL/100); +}