![]() ![]() With just a few changes to OP's code, iterate rather than replicate code with increasing power-of-ten. Rather than limit to pi != 3.14xxx, use a tolerance about the value. Give that a try to see if that it meets the spirit of your project. ![]() I utilized fixed #define statements, but you might perform some positional checking of a your test value to come up with a more flexible method of determining the precision value needed. The number of terms to get pi= 3.141000 is 443. When testing that out with a value of "3.14100", following is the last few output lines on the terminal. The main takeaway is the precision multiplier/divisor value to be used. Printf("The number of terms to get pi= %f is %d.\n", check, n-2) Pi=(round(pi*precision))/precision /* Provide enough precision */ Determine convergence by comparing successive values of the summation as you add. To make your code a bit more robust, following is a tweaked version of your code with some #define statements to identify the value being queried along with the precision to the right of the decimal place desired. Calculate pi, using a midpoint break loop. This statement never allows the code to review the values with a precision greater than two decimal places. In your code this statement is tripping you up. The reason for the apparent never ending process when you are looking for a value such as "3.14100" is that you need to up your rounding precision for more decimal places. Printf("The number of terms to get pi=3.14159 is %d.\n", n - 2) Printf("The number of terms to get pi=3.1415 is %d.\n", n - 2) Printf("The number of terms to get pi=3.141 is %d.\n", n - 2) (I have to do this without using prec ,trunc or some other features as I am not allowed) Please help me in this regard.(In C language) Actually I want that the table should print till I get 3.14,then I should get the statement showing the term number where I get 3.14, the the table starts from where I left and as soon as I get 3.141,the statement showing the term number where I get 3.141 prints and so on. This is not the exact output I want but kind of. This code works, and shows that we would get 3.14 at term 30 but when I repeat this code to get the term numbers where pi=3.141,pi=3.1415 and pi=3.14159, the code doesn't work and on execution just shows black screen with the statement, Calculating the value of pi. Printf("The number of terms to get pi=3.14 is %d.\n", n - 2) Printf("Calculating the value of pi.\n") How many terms of this series do you have to use before you first get 3.14? Shows the value of π approximated by one term of this series, by two terms, by three terms,Īnd so on. The question is: Calculate the value of π from the infinite series. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |