Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:");

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

      }

    }

}
Posted
Updated 30-Dec-21 7:02am
v2
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?

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.
 
Share this answer
 
Thank you. Looks fine. Leibniz would be happy.

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

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

:)
Shams Imad 30-Dec-21 11:59am    
I don't understand, can you explain to me your previous comment?

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