To be written after paper and pencil test is turned in. You may not consult othe
ID: 3805667 • Letter: T
Question
To be written after paper and pencil test is turned in. You may not consult other files saved on your computer for this part of the test. You may write this entire program in function main although functions are certainly permissible. The value of pi(Pi) can be estimated by the following formula for m+1 terms: pi approximately squareroot 12 Sigma^m_n=0 (-3)^-pi/(2n + 1) where n = 0, 1, 2, ellipsis m Thus the sum in the approximation is (-3)^0/(2*0+1) + (-3)^-1/(2*1+1) + ellipsis + (-3)^-m/(2*m+1) a. Write a program that uses this formula to calculate pi. In function main prompt the uses for the number of terms to be used in the approximation. Restrict the number of terms to an integer between 10 and 40 (including 10 and 40) terms by using a while or do ellipsis while data validation loop. Then calculate the approximation value for pi, using a for loop for the sum. Print the approximation with 6 decimal digits. b. In the same program create a table of radius values from 2 to 20 at intervals of 2 and corresponding circle circumferences (C = 2 pi r), using the value of pi from the approximation formula found in part a. (If you could not get part a to work, you may use 3.14159 as the value of pi for this part.) Use integers for radius and floats for circumference values. Place column headings above the table and print radius values in the column on the left with 0 decimal digits and circumferences in the column on the right with 2 decimal digits.Explanation / Answer
Please refer below code
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main()
{
int m,i,rad=2;
double sum = 0.0,temp;
double final_val;
do
{
printf(" Please enter number of terms between 10 and 40 inclusive : ");
scanf("%d", &m);
}while(m < 10 || m > 40);
for(i = 0; i < m; i++)
{
temp = pow(-3.0, -1*i)/((2 * i) + 1); //evaluating the term
sum += temp; //doing summation
}
final_val = sqrt(12.0) * sum; //calculating final value of pi
printf(" Value of pi : %f ", final_val);
rad = 2;
printf(" radius circumference ");
for(i = 0; i < 10; i++)
{
sum = 2 * final_val * rad; //circumference of circle
printf("%d %f ", rad,sum);
rad += 2;
}
return 0;
}
Please refer below output
Please enter number of terms between 10 and 40 inclusive : 2
Please enter number of terms between 10 and 40 inclusive : 25
Value of pi : 3.141593
radius circumference
2 12.566371
4 25.132741
6 37.699112
8 50.265482
10 62.831853
12 75.398224
14 87.964594
16 100.530965
18 113.097336
20 125.663706
Process returned 0 (0x0) execution time : 4.856 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.