Exercise 2-1 Programming in C is Fun!

#include <stdio.h> int main() { printf("Programming in C is fun!" ); return 0; }Copy the code

Practice 2-3 output inverted triangle pattern

#include <stdio.h>

int main()
{
    printf("* * * *\n");
    printf(" * * *\n");
    printf("  * *\n");
    printf("   *\n");
    
    return 0;
}
Copy the code

Practice 2-4 temperature conversion

#include <stdio.h>

int main()
{
    int fahr,celsius;
    fahr = 150;
    celsius = 5 * (fahr - 32 ) / 9;
    printf("fahr = %d, celsius = %d",fahr,celsius);
    return 0;
}
Copy the code

Exercise 2-6 Calculating how far objects fall (wrong)

#include <stdio.h> int main() { int t = 3; Printf (" height = % lf ", 100-0.5 * 10 * t * t); return 0; }Copy the code

Exercise 2-8 calculating the Celsius temperature

#include<stdio.h>

int main(){
  int far;
  int cel;
  scanf("%d",&far);
  cel = 5 * ( far - 32)/9;
  printf("Celsius = %d",cel);
  return 0;
}
Copy the code

Practice four integers from 2 to 9

#include<stdio.h>

int 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;
}
Copy the code

Exercises 2-10 Computing piecewise Functions [1]

#include<stdio.h>

int 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;
}
Copy the code

Exercise 2-11 Computing piecewise Functions [2]

#include<stdio.h> #include<math.h> 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; Elseif f = pow(x+1,2) + 2*x +1 /x; break; } printf("f(%.2lf) = %.2lf",x,f); return 0; }Copy the code

Exercise 2-12 Output Fahrenheit – Celsius temperature conversion table (error)

#include<stdio.h> int 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.1 f \ n ", fahr, cel); fahr +=2; }while(fahr <= upper); } else printf("Invalid."); return 0; }Copy the code

#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;
}
Copy the code

Exercise 2-14 Finding the sum of the first N terms of an odd one over sequence (error)

#include<stdio.h>

int main(){
  int n;
  double sum = 0;
  scanf("%d",&n);
  do{
    sum += 1/n;
    n--;
  }while(n > 0);
  printf("%.6lf",sum);
  return 0;
}
Copy the code

Exercise 2-15 Finding the sum of the first N terms of a simple staggered sequence (error)

#include<stdio.h>

int 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;
}
Copy the code