c primer plus 5编程练习
1、
#include <stdio.h> #define M_PER_HOUR 60 int main(void) { int min; int hour, min_left; printf("please input the minutes to convert: "); scanf("%d", &min); while(min > 0) { hour = min / M_PER_HOUR; min_left = min % M_PER_HOUR; printf("%d minutes is %d hour and %d min.\n", min, hour, min_left); printf("please input minutes to convert again: "); scanf("%d", &min); } return 0; }
2、
#include <stdio.h> int main(void) { int num; int end; printf("please input an integer: "); scanf("%d", &num); end = num + 10; while(num <= end) { printf("%d\t", num); num++; } return 0; }
3、……
#include <stdio.h> #define DAYS_PER_WEEK 7 int main(void) { int days; int week, days_left; printf("please input days: "); scanf("%d", &days); while(days > 0) { week = days / DAYS_PER_WEEK; days_left = days % DAYS_PER_WEEK; printf("%d days is %d week and %d days.\n", days, week, days_left); printf("please input days again: "); scanf("%d", &days); } return 0; }
4、
#include <stdio.h> #define CM_PER_FEET 36.4 //??? #define CM_PER_INCHES 15.5 // ??? int main(void) { float height; float feet, inches; printf("please input height: "); scanf("%f", &height); while(height > 0) { feet = height / CM_PER_FEET; inches = height / CM_PER_INCHES; printf("height in cm: %.1f.\n", height); printf("height in feet: %.1f\n", feet); printf("height in inches: %.1f\n", inches); printf("please input height again: "); scanf("%f", &height); } return 0; }
5、
#include <stdio.h> int main(void) { int count, sum; int days; count = 0; sum = 0; printf("please input the days: "); scanf("%d", &days); while(count++ < days) sum = sum + count; printf("sum = %d\n", sum); return 0; }
6、
#include <stdio.h> int main(void) { int count, sum; int days; int tmp; count = 0; sum = 0; printf("please input the days: "); scanf("%d", &days); while(count++ < days) { tmp = count * count; sum = sum + tmp; } printf("sum = %d\n", sum); return 0; }
7、
#include <stdio.h> void cubic(double n); int main(void) { double num; printf("please input an num: "); scanf("%lf", &num); cubic(num); return 0; } void cubic(double n) { printf("cubic of %.2f: %.2f.\n", n, n * n * n); }
8、
#include <stdio.h> int main(void) { int num1, num2; int remainder; printf("please input num1: "); scanf("%d", &num1); printf("please input num2: "); scanf("%d", &num2); while(num2 > 0) { remainder = num2 % num1; printf("%d %% %d is %d.\n", num2, num1, remainder); printf("please input num2 again: "); scanf("%d", &num2); } return 0; }
9、
#include <stdio.h> void Temperatures(double n); int main(void) { double tempera; int test; printf("please input the temperature: "); test = scanf("%lf", &tempera); while(test == 1) { Temperatures(tempera); printf("please input the temperature again: "); test = scanf("%lf", &tempera); } return 0; } void Temperatures(const double n) { double shishetem, kaishitem; shishetem = 5.0 / 9.0 * (n - 32.0); kaishitem = n + 273.16; printf("huashitem: %.2f.\n", n); printf("shishitem: %.2f.\n", shishetem); printf("kaishitem: %.2f.\n", kaishitem); }