0

I tried by first asking how many sigs they would like and then substituted in front of f where a number would be. Pls advise me!

#include <stdio.h>
#include <math.h>

int main(void) {
    int numberofsigs;
    printf("How many significant figures would you like in your pi: ");
    scanf("%d", &numberofsigs);
    printf(" +&.(%d)f\n", M_PI,numberofsigs); 
    return 0;
}

Thank you.

Banana
  • 3
  • 3
  • 1
    You need to do it in two steps, first build the formatting string and then print pi using the formatting string. – fredrik Jul 21 '21 at 08:57
  • 3
    The [`printf`](https://en.cppreference.com/w/c/io/fprintf) function have built-in ability to provide precision as an argument. – Some programmer dude Jul 21 '21 at 08:58
  • 3
    @fredrik That's incorrect. You can use `*` to specify the precision instead of a number. – AKX Jul 21 '21 at 08:59
  • 1
    `printf("%.*f\n", numberofsigs - 1, M_PI);` knowing that there is a leading `3`. But, you are limited by the precision of `double` and the `#define` if this is a typical sort of challenge to print pi to X significant digits. – Weather Vane Jul 21 '21 at 09:02
  • Assuming you're asking this question because you don't know how to find the information yourself, you should get hold of a C reference, and look up the definition of printf. It can be a bit confiusing and complicated -- but if you're stuck or confused by the standard text you have a better stackoverflow answer. Often you can substitute a google search for a C reference, because various people have put the C standard library specification online. – Paul Hankin Jul 21 '21 at 09:02

0 Answers0