web123456

Introductory C Exercises Series III (with Answers)

21 Programming Approximate Values of Specified Equations

Write a program that calculates the approximation (until the absolute value of the last term is less than the value ofeps) (eps is a very small value, either entered by the user or as a constant; the absolute value of x entered by the user should be less than 1, otherwise the loop cannot be terminated)

  1. #include <>
  2. #include <>
  3. #include <>
  4. int main()
  5. {
  6. double sum,eps=0.0000000001,x,t;
  7. int n;
  8. scanf("%lf",&x);
  9. sum=x;
  10. t=x;
  11. for(n=1;t>=eps;n=n+2)
  12. {
  13. t=-1*t*n/(n+1)*x*x/(n+3);
  14. sum+=x;
  15. }
  16. printf("sum=%lf\n",sum);
  17. return 0;
  18. }

22 Take all the odd numbers in a given integer and form a new number according to the rule

Take all the odd numbers in an unsigned decimal integer and form a new number in the original order.

  1. #include <>
  2. int main()
  3. {
  4. long x,s=0,j=0; //x:input data; s:reverse order of output data; j:output data;
  5. int n,i; //n:x the single digit shifted to the left from the first digit; i:s the single digit shifted to the left from the first digit;
  6. scanf("%ld",&x);
  7. //This problem is simpler to solve using bitwise arithmetic, which is done at the beginning, violently
  8. while(x) //x!=0
  9. {
  10. n=x%10;
  11. if(n%2!=0)
  12. s=s*10+n;
  13. x=x/10;
  14. }
  15. while(s)
  16. {
  17. i=s%10;
  18. j=j*10+i;
  19. s=s/10;
  20. }
  21. printf("%ld",j);
  22. }

23 Programming the output of characters as a rule

Write a program that performs the following function:Receive input from the keyboard until the Enter key is pressed, and for these letters convert them according to the following rules.

① If you entered uppercase letters, please convert them to lowercase letters; if you entered lowercase letters, please convert them to uppercase letters.

② For non-alphabetic characters, no conversion is performed and they are output as is.

③ If there is more than one consecutive space, only one space is output.

Please convert the input information according to the above rules before outputting it on the screen.

  1. #include <>
  2. int main()
  3. {
  4. char c,front= '\0';
  5. scanf("%c",&c);
  6. while(c!= '\n')
  7. {
  8. if(c!=' ')
  9. {
  10. if(c>='A'&&c<='Z')
  11. {
  12. c=c+32;
  13. putchar(c);
  14. }
  15. else if(c>='a'&&c<='z')
  16. {
  17. c=c-32;
  18. putchar(c);
  19. }
  20. else
  21. putchar(c);
  22. }
  23. if(c== ' ')
  24. if(c!=front)
  25. putchar(c);
  26. front=c;
  27. c=getchar();
  28. }
  29. return 0;
  30. }

24 Programming Statistics for Student Achievement

Enter the grades of a number of students from the keyboard, statistically output the sum of all the students' grades, and calculate the average grade of those students.

  1. #include <>
  2. int main()
  3. {
  4. float s,i=0; //s: single score; i: number of persons
  5. float avg=0,sum=0;
  6. scanf("%f",&s);
  7. while(s>=0) //Enter any complex number to exit the loop
  8. {
  9. i++;
  10. sum=sum+s;
  11. avg=sum/i;
  12. scanf("%f",&s);
  13. }
  14. printf("\nsum=%f\navg=%f\n",sum,avg);
  15. return 0;
  16. }

25 Enter an unsigned integer and sum over all digits.

  1. #include <>
  2. int main()
  3. {
  4. int x,m,s=0;
  5. scanf("%d",&x);
  6. while(x!=0)
  7. {
  8. m=x%10;
  9. s=s+m;
  10. x=x/10;
  11. }
  12. printf("%d",s);
  13. return 0;
  14. }

26 Finding the roots of a quadratic equation

Find the roots of a quadratic equation with 3function (math.)Find the roots of the discriminant when it is greater than 0, equal to 0, and less than 0, respectively, and output the results. Enter the coefficients a, b, and c in the main function.

  1. #include <>
  2. #include <>
  3. int main()
  4. {
  5. double a,b,c,delta,x1,x2,p,q;
  6. scanf("%lf %lf %lf",&a,&b,&c);
  7. if(a==0)
  8. printf("Not a quadratic equation! \n");
  9. else
  10. {
  11. delta=b*b-4*a*c;
  12. if(delta==0)
  13. {
  14. printf("The equation has two equal roots in real numbers! \n");
  15. x1=-b/(2*a);
  16. x2=x1;
  17. printf("%.2lf , %.2lf\n",x1,x2);
  18. }
  19. else if(delta>0)
  20. {
  21. printf("The equation has two unequal roots in real numbers! \n");
  22. x1=-b/(2*a)+sqrt(delta)/(2*a);
  23. x2=-b/(2*a)-sqrt(delta)/(2*a);
  24. printf("%.2lf , %.2lf\n",x1,x2);
  25. }
  26. else
  27. {
  28. printf("The equation has two unequal complex roots! \n");
  29. p=-b/(2*a);
  30. q=sqrt(-delta)/(2*a);
  31. printf("%.wlf + %.2lfi\n",p,q);
  32. printf("%.wlf - %.2lfi\n",p,q);
  33. }
  34. }
  35. return 0;
  36. }

27recursive (calculation)Methods to find the value of n-solution Legendre polynomials

  1. #include <>
  2. #include <>
  3. unsigned int getfactorial(unsigned int n,unsigned int x);
  4. int main()
  5. {
  6. unsigned int x,n,p;
  7. scanf("%u %u",&n,&x);
  8. p=getfactorial(n,x);
  9. printf("p(%u)=%u\n",x,p);
  10. return 0;
  11. }
  12. unsigned int getfactorial(unsigned int n,unsigned int x)
  13. {
  14. unsigned int result;
  15. if(n==0)
  16. result=1;
  17. else if(n==1)
  18. result=x;
  19. else if(n>1)
  20. result=((2*n-1)*x*getfactorial(n-1,x)-(n-1)*getfactorial(n-2,x))/n;
  21. return result;
  22. }

28 Calculation of bank balances and interest

Calculate Bank Balance and Interest:Assuming that the quarterly interest rate on bank deposits is 5.3%, calculate the interest and account balance based on the input raw data and output the interest and account balance for each quarter in tabular form. You are required to write two functions, one for calculating interest and balance and one for output.

  1. #include <>
  2. double function(float);
  3. void display(float,int);
  4. int main()
  5. {
  6. float sum;
  7. int season;
  8. scanf("%f %d",&sum,&season);
  9. display(sum,season);
  10. return 0;
  11. }
  12. double function(float sum)
  13. {
  14. float a,acc;
  15. a=0.053;
  16. acc=sum*a;
  17. return acc;
  18. }
  19. void display(float sum,int season)
  20. {
  21. int i;
  22. printf("Quarterly interest Account balance\n");
  23. printf("--------------------------\n");
  24. for(i=1;i<=season;i++)
  25. {
  26. printf("%d %f %f\n",i,function(sum),function(sum)*i+sum);
  27. /*Interest = Principal x Rate x Time*/
  28. printf("----------------------------\n");
  29. }
  30. }

29 Summing using functions and using the result as a function return value

  1. #include <>
  2. double fun(void);
  3. int main()
  4. {
  5. printf("Sum of the series: %lf\n",fun());
  6. return 0;
  7. }
  8. double fun()
  9. {
  10. int n=1;
  11. double term,sum=0.0;
  12. do
  13. {
  14. term=(double)(2*n-1)/(2*n*2*n);
  15. sum=sum+term;
  16. n++;
  17. }while(term>0.0001);
  18. return sum;
  19. }

30 Use a function to find the lower n-1 digits of w

Let w be an unsigned integer greater than 10, if w is an integer with n (n>=2) bits, write a function to find the number of low n-1 bits of w as the return value of the function. If w = 5923, the function returns 923.

  1. #include <>
  2. int fun(unsigned int);
  3. int main()
  4. {
  5. unsigned int w;
  6. scanf("%u",&w);
  7. if(w<10)
  8. {
  9. printf("data error!");
  10. }
  11. else
  12. printf("%d",fun(w));
  13. return 0;
  14. }
  15. int fun(unsigned int w)
  16. {
  17. int r=1,n=1,s;
  18. s=w;
  19. while(s>10)
  20. {
  21. s=s/10;
  22. r=r*10;
  23. n++;
  24. }
  25. w=w%r;
  26. return w;
  27. }