Home > Software design >  In C language how to calculate the term number of infinite series where we get pi=3.14, pi=3.141,pi=
In C language how to calculate the term number of infinite series where we get pi=3.14, pi=3.141,pi=

Time:12-01

The question is: Calculate the value of π from the infinite series. Print a table that shows the value of π approximated by one term of this series, by two terms, by three terms, and so on. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159?

int n = 2;
double sum,
 pi = 4,
    den;

printf("Calculating the value of pi.\n");
while (pi != 3.140000) {
    if (n % 2 == 0) {
        den = (2 * n) - 1;
        sum = (4.0 / den);
        pi = pi - sum;
    }
    else {
        den = (2 * n) - 1;
        sum = (4.0 / den);
        pi = pi   sum;
    }
    pi = (round(pi * 100)) / 100;
    printf("pi=%lf\n", pi);
    if (pi == 3.140000) {
        break;
    }
    n = n   1;
}

printf("The number of terms to get pi=3.14 is %d.\n", n - 2);

This code works, and shows that we would get 3.14 at term 30 but when I repeat this code to get the term numbers where pi=3.141,pi=3.1415 and pi=3.14159, the code doesn't work and on execution just shows black screen with the statement, Calculating the value of pi.This is not the exact output I want but kind of. Actually I want that the table should print till I get 3.14,then I should get the statement showing the term number where I get 3.14, the the table starts from where I left and as soon as I get 3.141,the statement showing the term number where I get 3.141 prints and so on. Please help me in this regard.(In C language) (I have to do this without using prec ,trunc or some other features as I am not allowed)

Edit: I did this:

int
main()
{
    int n = 2,
        x = 2,
        y = 2,
        z = 2;
    double sum,
     pi = 4;

    printf("Calculating the value of pi.\n");
    while (pi != 3.140000) {
        if (n % 2 == 0) {
            sum = (4.0 / ((2 * n) - 1));
            pi = pi - sum;
        }
        else {
            sum = (4.0 / ((2 * n) - 1));
            pi = pi   sum;
        }
        pi = (round(pi * 100)) / 100;
        if (pi == 3.140000) {
            break;
        }
        n = n   1;
    }
    while (pi != 3.141000) {
        if (n % 2 == 0) {
            sum = (4.0 / ((2 * n) - 1));
            pi = pi - sum;
        }
        else {
            sum = (4.0 / ((2 * n) - 1));
            pi = pi   sum;
        }
        pi = (round(pi * 100)) / 100;
        if (pi == 3.141000) {
            break;
        }
        n = n   1;
    }
    while (pi != 3.141500) {
        if (n % 2 == 0) {
            sum = (4.0 / ((2 * n) - 1));
            pi = pi - sum;
        }
        else {
            sum = (4.0 / ((2 * n) - 1));
            pi = pi   sum;
        }
        pi = (round(pi * 100)) / 100;
        if (pi == 3.141500) {
            break;
        }
        n = n   1;
    }
    while (pi != 3.141590) {
        if (n % 2 == 0) {
            sum = (4.0 / ((2 * n) - 1));
            pi = pi - sum;
        }
        else {
            sum = (4.0 / ((2 * n) - 1));
            pi = pi   sum;
        }
        pi = (round(pi * 100)) / 100;
        if (pi == 3.141590) {
            break;
        }
        n = n   1;
    }
    printf("The number of terms to get pi=3.14 is %d.\n", n - 2);
    printf("The number of terms to get pi=3.141 is %d.\n", n - 2);
    printf("The number of terms to get pi=3.1415 is %d.\n", n - 2);
    printf("The number of terms to get pi=3.14159 is %d.\n", n - 2);
}

but screen shows no output.

CodePudding user response:

while ( pi != 3.140000 ) requires that you know the answer already. Why would you be calculating pi if you did?

What you should use instead is something like while ( fabs( previous_pi - pi ) < 0.01/2 ).

And this gives us the answer to your question. Just vary 0.01 to get different precisions.

CodePudding user response:

The reason for the apparent never ending process when you are looking for a value such as "3.14100" is that you need to up your rounding precision for more decimal places. In your code this statement is tripping you up.

pi=(round(pi*100))/100;

This statement never allows the code to review the values with a precision greater than two decimal places.

To make your code a bit more robust, following is a tweaked version of your code with some #define statements to identify the value being queried along with the precision to the right of the decimal place desired.

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

#define check 3.14100
#define precision 1000

int main()
{
    int n=2;
    double sum,pi=4.000000,den;

    printf("Calculating the value of pi.\n");
    while(pi != check)
    {
        if(n%2==0)
        {
            den=(2*n)-1;
            sum=(4.0/den);
            pi=pi-sum;
        }
        else
        {
            den=(2*n)-1;
            sum=(4.0/den);
            pi=pi sum;
        }
        pi=(round(pi*precision))/precision; /* Provide enough precision */
        printf("pi=%lf\n",pi);
        if(pi == check)
        {
            break;
        }
        n=n 1;
    }
    printf("The number of terms to get pi= %f is %d.\n", check, n-2);

    return 0;
}

The main takeaway is the precision multiplier/divisor value to be used.

When testing that out with a value of "3.14100", following is the last few output lines on the terminal.

pi=3.142000
pi=3.137000
pi=3.141000
The number of terms to get pi= 3.141000 is 443.

I utilized fixed #define statements, but you might perform some positional checking of a your test value to come up with a more flexible method of determining the precision value needed.

Give that a try to see if that it meets the spirit of your project.

CodePudding user response:

Rather than limit to pi != 3.14xxx, use a tolerance about the value.

fabs(pi - pi_Appoximate) >= 0.5 / limit

With just a few changes to OP's code, iterate rather than replicate code with increasing power-of-ten.

int main(void) {
  printf("Calculating the value of pi.\n");
  char *pi_string = "3.1415926535897932384626433832795";
  for (unsigned i = 4; i<17; i  ) {
    char buf[i   1];
    buf[0] = '\0';
    double pi_Appoximate = atof(strncat(buf, pi_string, i));
    double limit = pow(10, i - 1);
    unsigned n = 2;
    double sum;
    double pi = 4;
    printf("i:%2u limit:%e ~pi:%.*g", i, limit, i, pi_Appoximate);
    fflush(stdout);
    while (fabs(pi - pi_Appoximate) >= 0.5 / limit) {
      if (n % 2 == 0) {
        sum = (4.0 / ((2 * n) - 1));
        pi = pi - sum;
      } else {
        sum = (4.0 / ((2 * n) - 1));
        pi = pi   sum;
      }
      pi = (round(pi * limit)) / limit;
      n = n   1;
    }
    printf(" pi:%.*g n:%u\n", i, pi, n);
  }
  puts(pi_string);
}

Output

Calculating the value of pi.
i: 4 limit:1.000000e 03 ~pi:3.14 pi:3.14 n:802
i: 5 limit:1.000000e 04 ~pi:3.141 pi:3.141 n:1145
i: 6 limit:1.000000e 05 ~pi:3.1415 pi:3.1415 n:36366
i: 7 limit:1.000000e 06 ~pi:3.14159 pi:3.14159 n:72729
i: 8 limit:1.000000e 07 ~pi:3.141592 pi:3.141592 n:1379312
i: 9 limit:1.000000e 08 ~pi:3.1415926 pi:3.1415926 n:2649009
i:10 limit:1.000000e 09 ~pi:3.14159265 pi:3.14159265 n:7476638
i:11 limit:1.000000e 10 ~pi:3.141592653 pi:3.141592653 n:40363271
i:12 limit:1.000000e 11 ~pi:3.1415926535 pi:3.1415926535 n:58402689
i:13 limit:1.000000e 12 ~pi:3.14159265358 pi:3.14159265358 n:62874301
i:14 limit:1.000000e 13 ~pi:3.141592653589 pi:3.141592653589 n:68508285
i:15 limit:1.000000e 14 ~pi:3.1415926535897 pi:3.1415926535897 n:53634827
i:16 limit:1.000000e 15 ~pi:3.14159265358979 pi:3.14159265358979 n:59359529
3.1415926535897932384626433832795

As double math precision is about 15-17 decimal digits, little need to go further.

  • Related