Skip to main content

Section 18.11 Practice Your Rounding!

Activity 18.5.

Write a function roundToTwo() that rounds off a floating point value to two decimal places. For example, 5.567 should be rounded to 5.67 and 5.534 should become 5.53.
The prototype for this function should be
double roundToTwo(double);
Please complete the code below.
Challenge: Round to “n” decimal places (you’ll have to pass an additional integer n to the function).

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void) {
    double num = 5.567;
    double rounded;
    
    rounded = roundToTwo(num);
    printf("The number %f, rounded to two decimal places is %f.\n", num, rounded);
    
    return 0;
}

/* Function definitions here - don't forget your prototype! */