Complementary task for topic: 1
M Nemeth · 2023-08-29 15:21:04.601220'
Printf: Real numbers III.
Printf: Real numbers III.
Calculate and print the result of a mathematical operations (sum, difference, product) with specific precision (precision: 2 digits, 4 digits and 1 digit after decimal point)
Hint: Use %f for float print, you can set the form with form specifier.
E.g.:%.2f will leave 2 digits after the decimal point
Solution
#include
int main() {
float num1 = 10.5;
float num2 = 3.2;
float sum = num1 + num2;
float difference = num1 - num2;
float product = num1 * num2;
printf("Result: %.2f, %.4f, %.1f\n", sum, difference, product);
return 0;
}
Explanation
In this example, we have two predefined float variables num1 and num2 set to 10.5 and 3.2, respectively. The program calculates the sum, difference, and product of the two numbers and stores them in the variables sum, difference, and product. Then, it uses printf() to display the results with different format specifiers and precision (%.2f, %.4f, %.1f).