Please write code for following C program The value of pi pi

Please write code for following C program

The value of pi (pi) can be estimated by the following formula: pi = 4 - 4/3 + 4/5 - 4/7 4/9 - 4/11 + ... Write a program that uses this formula to calculate pi. In function main prompt the user for the number of terms (limit) to be used in the approximation. Restrict the number of terms to an integer between 20 and 100 (including 20 and 100) terms by using a data validation loop. Then calculate the approximation value for pi, using a for loop for the sum. Print the approximation with 6 decimal digits. Test with 75 terms. In the same program create a table of radius values from 1 to 10 and corresponding sphere volumes, 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 volumes. Place column headings above the table and print radius values in the column on the left with 0 decimal digits and volumes in the column on the right with 3 decimal digits.

Solution

Answer A

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

int main()
{
/* declare sum variable as double */
double sum=0.00;

/* declare limit variable as int */
int limit;

printf(\"Enter the number of terms(limit) between 20 and 100 :\");

/* read limit from user*/
scanf(\"%d\",&limit);
int i;
/* check enter limit is between 20 and 100 */
if( (limit>=20 )&& (limit <=100))
{

for(i=1;i<=limit;i++)
{
if(i%2==0)
{
sum = sum - (double)4.0/(double)(2*i-1);
}
else
{
sum = sum + (double)4/(double)(2*i-1);

}
}

printf(\"Approximat value of pi is : %lf \ \",sum);
}
else
{
printf(\"please enter the number of terms(limit) between 20 and 100 \ \");
}


return 0;
}

-------------------------------------------------------------------------------------

output sample 1:-

Enter the number of terms(limit) between 20 and 100 :5
please enter the number of terms(limit) between 20 and 100

-------------------------

output sample 2:-

Enter the number of terms(limit) between 20 and 100 :20
Approximat value of pi is : 3.091624

---------------------------

output sample 3:-

Enter the number of terms(limit) between 20 and 100 :70
Approximat value of pi is : 3.127308

-------------------------

output sample 4:-

Enter the number of terms(limit) between 20 and 100 :100
Approximat value of pi is : 3.131593

--------------------------------

output sample 5-

Enter the number of terms(limit) between 20 and 100 :104
please enter the number of terms(limit) between 20 and 100

---------------------------------------------------------------------------------------------------------------------------------------------------

Answer B

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

int main()
{
/*radius as int type variable*/
int radius;

/*volume as float type variable*/
float volume;
printf(\"Radius Volume Table\ \ \");
printf(\"\\t Radius \\tVolume \ \");
/*for loop to calculate volume for radius 1 to 10*/
for(radius=1;radius<=10;radius++)
{
/*sphere volume formula*/
volume= (4.0/3.0)*3.14159*radius*radius*radius;

printf(\"\\t %d \\t%.3f\",radius,volume);
printf(\"\ \");

}
return 0;
}

--------------------

output sample:-

Radius Volume Table

Radius Volume
1 4.189
2 33.510
3 113.097
4 268.082
5 523.598
6 904.778
7 1436.754
8 2144.659
9 3053.625
10 4188.787

---------------------------------------------------------------------------------------------

If you have any query, please feel free to ask.

Thanks a lot.

Please write code for following C program The value of pi (pi) can be estimated by the following formula: pi = 4 - 4/3 + 4/5 - 4/7 4/9 - 4/11 + ... Write a prog
Please write code for following C program The value of pi (pi) can be estimated by the following formula: pi = 4 - 4/3 + 4/5 - 4/7 4/9 - 4/11 + ... Write a prog
Please write code for following C program The value of pi (pi) can be estimated by the following formula: pi = 4 - 4/3 + 4/5 - 4/7 4/9 - 4/11 + ... Write a prog

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site