Complementary task for topic: 1
M Nemeth · 2023-08-29 15:21:04.601220'
Printf: double-precision
Printf: double-precision
Calculate and print the average of three double-precision numbers with specific precision:
Write a C program that calculates the average of three predefined double-precision numbers and uses printf() to display the result with a specific precision.
Hint:
Use %f for float/doulbe print, you can set the form with form specifiers.
E.g.:%.2f will leave 2 digits after the decimal point.
Modern compilers understand %lf as well that is the same as %f. In the original standard there is no %lf for double, you can use only %f!
Warning: scanf() will not accept %f for double, just %lf!
Solution
#include
#include
int main() {
double num1 = 3.256;
double num2 = 2.187;
double num3 = 4.678;
double average = (num1 + num2 + num3) / 3;
printf("The average is %.3lf.\n", average);
return 0;
}
Explanation
In this example, we have three predefined double-precision float variables num1, num2, and num3 set to 3.256, 2.187, and 4.678, respectively. The program calculates the average of the three numbers and stores it in the average variable. Then, it uses printf() to display the result with three decimal places using the %lf format specifier with .3 precision. When you run the program, it will output the average of the three double-precision numbers with the specified precision using printf() as specified in the task.