Hello I was wondering if anyone could help me with this programming assignment i
ID: 3782818 • Letter: H
Question
Hello I was wondering if anyone could help me with this programming assignment in C++. Thanks in advance! : Pi, a mathematical constant commonly approximated as 3.14159, is the ratio of a circle's circumference to its diameter. It is usually represented by the Greek letter . Write a C++ program that approximates the value of pi using the Gregory-Leibniz formula. Since this formula is an infinite series, you will need to prompt the user for the number of iterations (summation terms) they want to use in the calculation. For example, if the user indicates "4" iterations, the result would be = 4 - 4/3 + 4/5 - 4/7 or approximately 2.89524. If the user indicates "10" iterations, the result would be = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15 + 4/17 - 4/19 or approximately 3.04184. If the user indicated "10000" iterations, the result would be approximately 3.14149. Thus, as the number of summation terms increases, the result converges on true value of PI. Rather than putting all your code in main(), use functions to perform the calculation. Embed your program in a loop so that the calculation can be repeated multiple times.
Explanation / Answer
#include<stdio.h>
int main()
{
double pi=0;
int number;
printf("Please enter number of iterations: ");
scanf("%d", &number);
int j=1;
for(int i=1; i<=number; i++)
{
if(i %2==1)
pi+=4/j;
else
pi-=4/j;
j=j+2;
}
printf("Value of pi is: %f", pi);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.