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
#include<stdio.h>
int main(){
double a,b,c,d,e,f,g;
printf("case1\n");
printf("please input five real number\n");
printf("No.1:");
scanf(" %lf", &a);
printf("\nNo.2:");
scanf(" %lf", &b);
printf("\nNo.3:");
scanf(" %lf", &c);
printf("\nNo.4:");
scanf(" %lf", &d);
printf("\nNo.5:");
scanf(" %lf", &e);
printf("\n The five number: %lf %lf %lf %lf %lf", a, b, c, d, e);
f = a+b+c+d+e;
g = f/5;
printf("\n sum: %lf", f);
printf("\n average: %lf", g);
printf("\n\n\ncase2\n");
printf("please input five real number\n");
printf("No.1:");
scanf(" %lf", &a);
printf("\nNo.1 is %lf",a);
printf("\n\nNo.2:");
scanf(" %lf", &b);
printf("\nNo.2 is %lf",b);
a = a+b;
printf("\n\nNo.3:");
scanf(" %lf", &b);
printf("\nNo.3 is %lf",b);
a = a+b;
printf("\n\nNo.4:");
scanf(" %lf", &b);
printf("\nNo.4 is %lf",b);
a = a+b;
printf("\n\nNo.5:");
scanf(" %lf", &b);
printf("\nNo.5 is %lf",b);
a = a+b;
b = a/5;
printf("\n sum: %lf", a);
printf("\n average: %lf", b);
return 0;
}
複製代碼
作者: Smallcat 時間: 2018-9-20 23:44 標題: 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?
#include<stdio.h>
int main(){
double a,b,c;
printf("Program to convert Fahrenheit to Celsius.\n");
printf("Fahrenheit temperature?");
scanf(" %lf", &a);
b = (a-32)*5/9;
printf("Celsius equivalent: %lf",b);
return 0;
}
複製代碼
作者: Smallcat 時間: 2018-9-21 09:11
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.
#include<stdio.h>
int main(){
int a,b,c,d,e,f;
printf("please input five-digit number.\n");
printf("No.1:");
scanf(" %d", &a);
printf("\nNo.2:");
scanf(" %d", &b);
printf("\nNo.3:");
scanf(" %d", &c);
printf("\nNo.4:");
scanf(" %d", &d);
printf("\nNo.5:");
scanf(" %d", &e);
f = a+b+c+d+e;
printf("\nthe sum of its digits: %d",f);
f = 10000*a + 1000*b + 100*c + 10*d +e;
f = f+11111;
printf("\na new number by adding one to each of its digits: %d",f);
return 0;
}
複製代碼
作者: Smallcat 時間: 2018-9-21 09:13
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.
#include<stdio.h>
int main(){
int a,b,c,d;
printf("please enter a length of time in seconds");
scanf(" %d", &a);
b = a/3600;
c = a%3600;
c = c/60;
d = a - 3600*b - 60*c;
printf("%d hours, %d minutes, %d seconds",b,c,d);
}
複製代碼
作者: Smallcat 時間: 2018-9-21 09:21
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)
#include<stdio.h>
int main(){
double a,b;
printf("please input the value of X : ");
scanf(" %lf", &a);
b = a*a*a + 5*a*a + 10*a + 15;
printf("answer: %lf", b);
}
複製代碼
作者: Smallcat 時間: 2018-9-21 09:34
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
#include<stdio.h>
int main (){
int fib1, fib2, fib3;
printf("A Fibonacci number is a member of a set \n \
in which each number is the sum of the previous two numbers. \n \
The Fibonacci series describes a form of a spiral. \n \
The series begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, … \n");
fib1 = 13 + 21;
fib2 = 21 + fib1;
fib3 = fib1 + fib2;
printf("\n\n the next three number are %d %d %d",fib1,fib2,fib3);