• §

    Jonas Altrock ew20b126@technikum-wien.at

    To overview.

    The whole source file u3_cube.c.

    /* Compile with `clang -std=c99 -Wall -o u3_cube u3_cube.c`
    /* Test with `../checkproject u3_cube` */
  • §

    Include C-libraries for input/output.

    #include <stdio.h>
    #include <stdlib.h>
  • §

    Exercise 3: Expressions

  • §

    Calculate the volume and the surface of a cube.

    Details

  • §

    Read the edge length of a cube (as a decimal number). Then print the volume and the surface area. The result should be printed with two digits after the decimal point.

    Example

  • §

    Input:

    2.5
    

    Output:

    Volume: 15.63
    Surface Area: 37.50
    

    main()

  • §
    int main() {
  • §

    We work width a double precision variable.

        double d;
  • §

    The scanf format code %lf means long float, which is a double.

        scanf("%lf", &d);
  • §

    Directly pass the calculation results to printf, ensuring two decimal places with the .2 format code prefix.

        printf("Volume: %.2lf\n", d * d * d);
        printf("Surface Area: %.2lf\n", 6 * d * d);
  • §

    Return code 0 for everything OK.

        return 0;
    }