Complementary task for topic: 1

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

Printf: Real number I.

Printf: Real number I.

Calculate and print the area of a rectangle with floating-point precision:
Write a C program that calculates the area of a rectangle with predefined floating-point width and height, and uses printf() to display the result with floating-point precision.

Hint: Use %f for float print

Solution
#include 

int main() {
    float width = 3.5;
    float height = 2.8;
    
    float area = width * height;
    
    printf("The area of the rectangle is %.2f.\n", area);
    
    return 0;
}


Explanation
In this example, we have predefined float variables width and height set to 3.5 and 2.8, respectively. The program calculates the area of the rectangle (width multiplied by height) and stores it in the area 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 area of the rectangle with floating-point precision using printf() as specified in the task.
< < previous    next > >