input n, compute f(n) keeping exactly 2 digits after the decimal point.
example: input 5 output: 15.00
(Improvements / suggetions to the code below welcome)Here’s what I ‘ve come up with so far;
#include <stdio.h>
#include <math.h>
int main() {
float m;
scanf("%f", &m);
if (m < 0)
m = abs(m);
else if (m < 2)
m = sqrt(m + 1);
else if (m < 4)
m = pow(m + 2, 5);
else if (m >= 4)
m = (m * 2);
m = m + 5;
printf("%1.2f", m);
}
You should use braces for all
ifblocks. That way you avoid mistakes like the one in your last case, which should be:or even better:
In addition, the first case can be written as:
although this is actually an optimization over the original formula and may or may not be more performant depending on the compiler and its options.