Thank you. Looks fine. Leibniz would be happy.

:)

15,901,122 members

See more:

Write a program that Calculate the value of π

from the infinite series

Print a table that shows the value of π approximated

by one term of this series, by two terms, by three

terms, and so on.

**What I have tried:**

from the infinite series

Print a table that shows the value of π approximated

by one term of this series, by two terms, by three

terms, and so on.

Output: Accuracy set at: 40 term pi 1 4.000000 2 2.666667 3 3.466667 4 2.895238 5 3.339683 6 2.976046 7 3.283738 8 3.017072 ... ... ... 40 3.116597

C#

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace picalc { class Program { static void Main(string[] args) { int accuracy; double piValue = 0; double numerator = 4.0; double denominator = 1.0; Console.Write("Enter the number of terms:"); accuracy = Convert.ToInt32(Console.ReadLine()); int i; Console.WriteLine("Term \t\tPi"); for (i = 1; i <= accuracy; i++) { double x = (numerator / denominator); if ((i % 2) == 0) { piValue -= x; } else { piValue += x; } denominator += 2.0; Console.WriteLine("{0}\t\t{1:F16}", i, piValue); } Console.ReadLine(); } } }

Comments

Patrice T
30-Dec-21 10:32am

And you have a question ?

Shams Imad
30-Dec-21 11:13am

yes,How can this question be solved?

Thank you. Looks fine. Leibniz would be happy.

:)

:)

Permalink

Share this answer

Comments

Shams Imad
30-Dec-21 11:14am

How can this question be solved?

Luc Pattyn
30-Dec-21 11:30am

I see one way to improve your code:

rather than asking the user how many iterations he wants (what you erroneously call "accuracy"), you could let the loop continue until the difference drops below a specified tolerance, say 1e-12

It then would be wise not to show all intermediate steps...

:)

rather than asking the user how many iterations he wants (what you erroneously call "accuracy"), you could let the loop continue until the difference drops below a specified tolerance, say 1e-12

It then would be wise not to show all intermediate steps...

:)

Shams Imad
30-Dec-21 11:32am

Can you write the code to solve this question?

Luc Pattyn
30-Dec-21 11:58am

Writing the code is your job, not mine.

You're on track; see my suggestion in my previous reply to you.

:)

You're on track; see my suggestion in my previous reply to you.

:)

Shams Imad
30-Dec-21 11:59am

I don't understand, can you explain to me your previous comment?

Luc Pattyn
30-Dec-21 12:08pm

Your pi approximations are slightly above, then slightly below, the correct value, as you alternately add/subtract a correction term. So it would be best to keep iterating until said correction becomes irrelevantly small, i.e. less than epsilon, which is a small number that must satisfy the relationship

which means it is still noticeable when using a number representation with finite precision. As

NB: that will take billions of iterations, since

1.0 + epsilon != 1.0

which means it is still noticeable when using a number representation with finite precision. As

`double`

is capable of holding about 15 decimals, epsilon = 10^-12 seems very reasonnable. That would give a pi approximation with some 12 correct digits.NB: that will take billions of iterations, since

`denominator`

only grows by 2 per iteration!Quote:Write a program that Calculate the value of π from the infinite series

As programmer, your job is to create solutions to given problems, as student, this homework is training/practicing and all homework makes early experience.

First of all you need to get informations about known methods to calculate pi with infinite decimals: Infinite Expressions for Pi[^]

If you take the pain to search internet, you will find pieces of code that do the job.

Permalink

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900