15,663,225 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.
```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```

What I have tried:

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:");

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);

}

}

}

}```
Posted
Updated 30-Dec-21 7:02am
v2
Patrice T 30-Dec-21 10:32am
And you have a question ?
yes,How can this question be solved?

## Solution 1

Thank you. Looks fine. Leibniz would be happy.

:)

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...

:)
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.

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

## Solution 2

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.