diff --git a/c_primer_plus/chapter05/02.c b/c_primer_plus/chapter05/02.c new file mode 100644 index 0000000..7a8d02b --- /dev/null +++ b/c_primer_plus/chapter05/02.c @@ -0,0 +1,27 @@ +#include + + + +int main (void) { + + int number; + int i; + + printf("Give a number: "); + + if (scanf("%d", &number) != 1) { + printf("Not a number!\n"); + return 1; + } + + i = number; + + while (i <= number + 10) { + printf("%d ", i++); + } + + printf("\n"); + + return 0; + +} \ No newline at end of file diff --git a/c_primer_plus/chapter05/03.c b/c_primer_plus/chapter05/03.c new file mode 100644 index 0000000..07f7f55 --- /dev/null +++ b/c_primer_plus/chapter05/03.c @@ -0,0 +1,39 @@ +#include + + + +#define DAYS_PER_WEEK 7 + + +unsigned getDays(void); + + +int main (void) { + + int days; + + days = getDays(); + + while (days != 0) { + printf("%d days are %d weeks, %d days\n", days, days / DAYS_PER_WEEK, days % DAYS_PER_WEEK); + days = getDays(); + } + + return 0; + +} + + +unsigned getDays(void) { + + int days; + + printf("Number of days (<=0 to quit): "); + + if (scanf("%d", &days) != 1 || days <= 0) { + days = 0; + } + + return days; + +} diff --git a/c_primer_plus/chapter05/04.c b/c_primer_plus/chapter05/04.c new file mode 100644 index 0000000..e01efbc --- /dev/null +++ b/c_primer_plus/chapter05/04.c @@ -0,0 +1,44 @@ +#include +#include + + +#define CM_PER_FEET 30.48 +#define CM_PER_INCH 2.54 + + +double getHeight(void); + + +int main (void) { + + double height; + + height = getHeight(); + + while (height != 0) { + unsigned feet = height / CM_PER_FEET; + double inch = (height - feet * CM_PER_FEET) / CM_PER_INCH; + printf("%.2f cm = %u feet, %.1f inches\n", height, feet, inch); + height = getHeight(); + } + + printf("bye\n"); + + return 0; + +} + + +double getHeight(void) { + + double height; + + printf("Enter a height in centimeters (<=0 to quit): "); + + if (scanf("%lf", &height) != 1 || height <= 0) { + height = 0; + } + + return height; + +}