Westonci.ca connects you with experts who provide insightful answers to your questions. Join us today and start learning! Explore comprehensive solutions to your questions from a wide range of professionals on our user-friendly platform. Discover in-depth answers to your questions from a wide network of professionals on our user-friendly Q&A platform.

Write a function using a loop to approximate the value of PI using the formula given including terms up through 1/99, 1/999 and 1/9999. As the number of iterations increase, the estimate gets closer to the value of PI. The function should accept the number of iterations and return the estimate of PI.

Sagot :

Answer:

Following are the code to the given question:

#include <stdio.h>//header file

double estPi(int precision)//defining a method estPi that accepts value in parameter  

{

   double pi = 0, sign = 1, n = 1;//defining a double variable

   while (n <= precision) //use while loop that checks n value less than equal to precision

   {

       pi += sign / n;//defining pi variable that holds quotient value

       sign *= -1;//holding value in sign value

       n += 2;//increment value in n

   }

   return 4 * pi;//return value

}

int main() //main method

{

   int n;//defining an integer variable

   printf("Enter number of iterations: ");//print message

   scanf("%d", &n);//input value

   printf("Estimated PI is %lf\n", estPi(n));//print method that calls method

   return 0;

}

Output:

Please find the attached file.

Explanation:

In the given code a method "estPi" is declared that holds an integer variable "precision" is declared inside the method multiple double variable is declared inside the loop it calculate the value and return its values.

Inside the main method an integer variable "n" is declared that use print method to input the value and accepting value from the user-end and passing value in the method and print its values.

View image codiepienagoya