Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program code for the following: Your program should stop itself when t

ID: 3750171 • Letter: W

Question

Write a C program code for the following:


Your program should stop itself when the precision is met and output the following: It took n terms to approximate pi to 3.14159.

For a tip: Think about a numerator and a denominator. You learned in a previous homework how to generate odd number sequences. Pay close attention to the sign of each term. Casting: You can eliminate all numbers beyond the 5th decimal position using casting, such as int a = (int)(3.14159265 * 100000). Here a float is being multiplied by an integer, with the result being a float. The decimal point has been moved 5 places to the right. Use (int) to "cast" that into an integer, truncating all the decimals of the new number, resulting in the new number 314159.

1 3 5 79 11

Explanation / Answer

#include<stdio.h>

int main() {
long int terms = 0;
int num = 4;
int den = 1;
int pi_val = 314159;
float temp;
int app_val_int;
float app_val = 0.0;
int sign=1;
while(1){
terms++;
temp = (sign)*((float)num/(float)den);
app_val = app_val + temp;
app_val_int = (int)(app_val * 100000);
if(app_val_int == pi_val){
break;
}
den = den + 2;
sign = sign * -1;
}
printf(" The no of terms required to approximate pi value is %d",terms);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote