Complementary task for topic: 1

M Nemeth · 2023-08-29 15:21:04.600220'

Printf: float II.

Printf: float II.

Calculate and print the average of three floating-point numbers with specific precision:
Write a C program that calculates the average of three predefined floating-point numbers and uses printf() to display the result with a specific precision.

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 = 3.2;
    float num2 = 2.5;
    float num3 = 4.8;
    
    float average = (num1 + num2 + num3) / 3;
    
    printf("The average is %.2f.\n", average);
    
    return 0;
}

Explanation
In this example, we have three predefined float variables num1, num2, and num3 set to 3.2, 2.5, and 4.8, 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 two decimal places using the %f format specifier with .2 precision.

When you run the program, it will output the average of the three floating-point numbers with the specified precision using printf() as specified in the task.
< < previous    next > >