免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 發帖

[重要物] c++練習題1

1.        Write a program that does the following:

a.        Prompts the user to input five floating-point numbers (real numbers).
b.        Prints the five floating-point numbers.
c.        Adds the five floating-point numbers.
d.        Prints the sum and average of the five floating-point numbers.

Case I: Complete the program by using any number of variables.
Case II: Complete the program just by using two variables
  1. #include<stdio.h>
  2. int main(){

  3. double a,b,c,d,e,f,g;

  4. printf("case1\n");
  5. printf("please input five real number\n");
  6. printf("No.1:");
  7. scanf(" %lf", &a);
  8. printf("\nNo.2:");
  9. scanf(" %lf", &b);
  10. printf("\nNo.3:");
  11. scanf(" %lf", &c);
  12. printf("\nNo.4:");
  13. scanf(" %lf", &d);
  14. printf("\nNo.5:");
  15. scanf(" %lf", &e);
  16. printf("\n The five number: %lf %lf %lf %lf %lf", a, b, c, d, e);

  17. f = a+b+c+d+e;
  18. g = f/5;

  19. printf("\n sum: %lf", f);
  20. printf("\n average: %lf", g);

  21. printf("\n\n\ncase2\n");
  22. printf("please input five real number\n");

  23. printf("No.1:");
  24. scanf(" %lf", &a);
  25. printf("\nNo.1 is %lf",a);

  26. printf("\n\nNo.2:");
  27. scanf(" %lf", &b);
  28. printf("\nNo.2 is %lf",b);
  29. a = a+b;

  30. printf("\n\nNo.3:");
  31. scanf(" %lf", &b);
  32. printf("\nNo.3 is %lf",b);
  33. a = a+b;

  34. printf("\n\nNo.4:");
  35. scanf(" %lf", &b);
  36. printf("\nNo.4 is %lf",b);
  37. a = a+b;


  38. printf("\n\nNo.5:");
  39. scanf(" %lf", &b);
  40. printf("\nNo.5 is %lf",b);
  41. a = a+b;

  42. b = a/5;

  43. printf("\n sum: %lf", a);
  44. printf("\n average: %lf", b);

  45. return 0;

  46.        
  47. }
複製代碼
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

c++練習題1

2.       
Write a program that reads in a temperature in degrees Fahrenheit
and prints the corresponding temperature in degrees Celsius. The conversion formula is
           C =  (F-32)
The following is a sample run of the program

If you write this program carelessly, the answer always comes out 0.
What bug causes this result?
  1. #include<stdio.h>
  2. int main(){

  3. double a,b,c;

  4. printf("Program to convert Fahrenheit to Celsius.\n");
  5. printf("Fahrenheit temperature?");
  6. scanf(" %lf", &a);
  7. b = (a-32)*5/9;
  8. printf("Celsius equivalent: %lf",b);

  9. return 0;

  10.        
  11. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

3.       
If a five-digit number is input through the keyboard:
(a). Write a program to calculate the sum of its digits.
(b). Write a program to print a new number by adding one to each of
      its digits. For example, if the input number 12391, then the outpu
      should be displayed as 23502.
  1. #include<stdio.h>
  2. int main(){

  3. int a,b,c,d,e,f;

  4. printf("please input five-digit number.\n");
  5. printf("No.1:");
  6. scanf(" %d", &a);
  7. printf("\nNo.2:");
  8. scanf(" %d", &b);
  9. printf("\nNo.3:");
  10. scanf(" %d", &c);
  11. printf("\nNo.4:");
  12. scanf(" %d", &d);
  13. printf("\nNo.5:");
  14. scanf(" %d", &e);
  15. f = a+b+c+d+e;
  16. printf("\nthe sum of its digits: %d",f);
  17. f = 10000*a + 1000*b + 100*c + 10*d +e;
  18. f = f+11111;
  19. printf("\na new number by adding one to each of its digits: %d",f);

  20. return 0;

  21.        
  22. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

4.        
write a program that allows the user to enter a length of time in seconds.
The program should then output the number of hours, minutes, and seconds that corresponds to that number of seconds.
For example, if the user inputs 50390 total seconds then the program should output 13 hours, 59 minutes, an 50 seconds.
  1. #include<stdio.h>
  2. int main(){
  3.         int a,b,c,d;
  4.         printf("please enter a length of time in seconds");
  5.         scanf(" %d", &a);
  6.         b = a/3600;
  7.         c = a%3600;
  8.         c = c/60;
  9.         d = a - 3600*b - 60*c;
  10.         printf("%d hours, %d minutes, %d seconds",b,c,d);
  11. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

5.       
Write a program to evaluate the polynomial:
f(x) =  x^3 + 5x^2  + 10x  + 15
Read the data for the x from the keyboard and output the value of f(x)
  1. #include<stdio.h>
  2. int main(){
  3.         double a,b;
  4.         printf("please input the value of X : ");
  5.         scanf(" %lf", &a);
  6.         b = a*a*a + 5*a*a + 10*a + 15;
  7.         printf("answer: %lf", b);
  8.        
  9. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

6.       
A Fibonacci number is a member of a set in which each number is the sum of the previous two numbers.
(The Fibonacci series describes a form of a spiral. )

The series begins:
0, 1, 1, 2, 3, 5, 8, 13, 21, …

Write a program that calculate and prints the next three numbers in the Fibonacci series.
You are to use only three variables: fib1, fib2, and fib3
  1. #include<stdio.h>
  2. int main (){
  3.         int fib1, fib2, fib3;
  4.         printf("A Fibonacci number is a member of a set \n \
  5. in which each number is the sum of the previous two numbers. \n \
  6. The Fibonacci series describes a form of a spiral.  \n \
  7. The series begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, … \n");
  8.         fib1 = 13 + 21;
  9.         fib2 = 21 + fib1;
  10.         fib3 = fib1 + fib2;
  11.        
  12.         printf("\n\n the next three number are %d %d %d",fib1,fib2,fib3);

  13. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

第7小題比較長,額外開一個新帖子。
在此

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

返回列表