I'm trying to output my results to the decimal accuracy the user has input. In other words, if they enter 145, then it outputs to 0 d.p. - if they enter 145.67, it outputs the result to 2 d.p.
I've tried achieving this by trying different things with %.lf but didn't manage to do it so far. Is there a way to do this in C? If so, is there a name for it - so I can learn more about it.
#include <math.h>
#include <stdio.h>
int main() {
// Using long, double or something else?
double number, fourthRoot;
printf("Enter a number: ");
scanf("%lf", &number);
fourthRoot = sqrt(sqrt(number));
printf("4th root of number %lf is %.10lf", number, fourthRoot);
return 0;
}
Read user input as a line with
fgets()or somescanf("%s",...-like code.And then process the string. One way is with
sscanf()This works OK but does get fooled if exponential notation used. Additional code could look for the
Eore(orP,pwith hexadecimal FP notation).