Click here to Skip to main content
15,868,119 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Dear all,

I'm new with programming C... Now I've to calculate the value of pi. It should be like this to approach pi:
pi = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ... )

Can anyone help me how to do it?

Thx
Posted
Updated 24-Sep-10 1:06am
v3
Comments
Sandeep Mewara 24-Sep-10 5:26am    
No effort

You can use a flag to determine if you are adding or subtracting the next value.

I will leave the implementation up to you ;)

Dave
 
Share this answer
 
The interesting point about calculating pi like this is how to terminate the loop. Ideally you want to keep accumulating terms until (within the number of significant figures you want) the nth and (n+2)th terms are the same within that number of figures.

Anyway I'd go back to your textbook or lecture notes and have a look at how you control loops, how you declare and modify variables and then try and bolt something together. Print each term as you do it and then post what you've got here if it's not working and someone will try and guide you through the bits you're not getting.

Cheers,

Ash

PS: As this is a homework assignment, remember CS lecturers have a recursion fetish so if you've covered recursion make sure you use it, however inappropriate it is in the context of a particular problem!
 
Share this answer
 
In c# Ystem.Math class has a PI property. You can use that as is
 
Share this answer
 
Comments
Afu112 24-Sep-10 3:54am    
Thx for your fast reply.
But in the instructions of school I've to approach it like above.

So it should be something like

> for (count = 1 count c = pi; count +=2.0)

pi = 3.14159265

Is this ok? And how to i get the "-" (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ... )?
DavidKiryazi 24-Sep-10 3:56am    
I think he means C not C#. I don't know why there is a C# tag if its not related to the C# language and provided framework
Toli Cuturicu 24-Sep-10 7:07am    
Oh, so it is a homework (school assignment). Then, this question has no place here.
Marc A. Brown 24-Sep-10 14:47pm    
I'd argue that the question AS IS has no place here, but if the OP runs into implementation trouble, asking a question related to that is fine. But we need to see that the OP has actually tried to do the work.
C#
static void Main(string[] args)
        {
            Console.WriteLine(PI(13));
            Console.Read();
        }
        static double PI(int max)
        {
            double pi=0;
            int sign = 1;
            for (int c = 1; c <= max; c += 2)
            {
                pi += sign * (1.0 / c);
                sign *= -1;
            }
            return 4 * pi;
        }
 
Share this answer
 
If the sum then use your formula
Pi = 4*sum((-1)^(n+1)*1/(2*n-1)); n=1;N
With high N

or
Pi = acos(-1.);
 
Share this answer
 
v2

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