练习2-1 Programming in C is fun!
#includeint main(){ printf("Programming in C is fun!"); return 0;}复制代码
练习2-3 输出倒三角图案
#includeint main(){ printf("* * * *\n"); printf(" * * *\n"); printf(" * *\n"); printf(" *\n"); return 0;}复制代码
练习2-4 温度转换
#includeint main(){ int fahr,celsius; fahr = 150; celsius = 5 * (fahr - 32 ) / 9; printf("fahr = %d, celsius = %d",fahr,celsius); return 0;}复制代码
练习2-6 计算物体自由下落的距离(错误)
#includeint main(){ int t = 3; printf("height = %lf",100-0.5*10*t*t); return 0;}复制代码
练习2-8 计算摄氏温度
#includeint main(){ int far; int cel; scanf("%d",&far); cel = 5 * ( far - 32)/9; printf("Celsius = %d",cel); return 0;}复制代码
练习2-9 整数四则运算
#includeint main(){ int a,b; scanf("%d %d",&a,&b); printf("%d + %d = %d\n",a,b,a+b); printf("%d - %d = %d\n",a,b,a-b); printf("%d * %d = %d\n",a,b,a*b); printf("%d / %d = %d\n",a,b,a/b); return 0;}复制代码
练习2-10 计算分段函数[1]
#includeint main(){ double x; double f; int type = 2; scanf("%lf",&x); if (x == 0) type = 0; switch(type){ case 0: f = 0; break; default: f = 1/x; break; } printf("f(%.1lf) = %.1lf",x,f); return 0;}复制代码
练习2-11 计算分段函数[2]
#include#include int main(){ double x; double f; scanf("%lf",&x); int type; if (x >= 0 ) type = 1; else type = 2; switch(type){ case 1: f = sqrt(x); break; case 2: f = pow(x+1,2) + 2*x + 1/x; break; } printf("f(%.2lf) = %.2lf",x,f); return 0;}复制代码
练习2-12 输出华氏-摄氏温度转换表(错误)
#includeint main(){ int lower,upper; int fahr; double cel; scanf("%d %d",&lower,&upper); if (lower <= upper){ fahr = lower; printf("fahr celsius\n"); do{ cel = 5.0*(fahr-32)/9.0; printf("%d %6.1f\n",fahr,cel); fahr +=2; }while(fahr <= upper); } else printf("Invalid."); return 0;}复制代码
练习2-13 求N分之一序列前N项和(错误) #include<stdio.h>
int main(){ int n; scanf("%d",&n); double sum = 0.0; int count = 1; do{ sum = sum + 1/count; count++; }while(count <= n); printf("sum = %.6lf",sum); return 0;}复制代码
练习2-14 求奇数分之一序列前N项和(错误)
#includeint main(){ int n; double sum = 0; scanf("%d",&n); do{ sum += 1/n; n--; }while(n > 0); printf("%.6lf",sum); return 0;}复制代码
练习2-15 求简单交错序列前N项和(错误)
#includeint main(){ int n; scanf("%d\n",&n); double sum; int i = 1; int k = 1; while(k < n){ sum += 1/i; i += 3; k++; } printf("sum = %.3f",sum); return 0;}复制代码