Browse Source

Add next prog exercises of chapter 05

master
T. Meissner 9 years ago
parent
commit
5b28b58d5c
3 changed files with 110 additions and 0 deletions
  1. +27
    -0
      c_primer_plus/chapter05/02.c
  2. +39
    -0
      c_primer_plus/chapter05/03.c
  3. +44
    -0
      c_primer_plus/chapter05/04.c

+ 27
- 0
c_primer_plus/chapter05/02.c View File

@ -0,0 +1,27 @@
#include <stdio.h>
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;
}

+ 39
- 0
c_primer_plus/chapter05/03.c View File

@ -0,0 +1,39 @@
#include <stdio.h>
#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;
}

+ 44
- 0
c_primer_plus/chapter05/04.c View File

@ -0,0 +1,44 @@
#include <stdio.h>
#include <float.h>
#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;
}

Loading…
Cancel
Save