I hate C, and I been forced to take it as part of my major (C for engineers) ,
Anyhow I have to write a quick program C to calculate PI
The program is this way
Write an interactive program that asks the user how many terms of the series equation to use in approximating pie. Then calculate and display the approximation.
The forumula they are talking about is
pi = 4X ( 1-1/3+1/5-1/7+1/9-1/11+1/13-....)
and so on.
Here is the example of the output they want:
Pi Approximation Program
How Many terms of the series should be included?
(The more terms, the better the approximation.)
=> 3
Approximate value of pi is 3.4667
what do I have so far? a non working version of the program that my small mind can't comprehend since C is a extra terrestial language.
this would be a lot easier in C++ since I have a strong idea how it works.
But of course I am lost with this problem
Anyhow I have to write a quick program C to calculate PI
The program is this way
Write an interactive program that asks the user how many terms of the series equation to use in approximating pie. Then calculate and display the approximation.
The forumula they are talking about is
pi = 4X ( 1-1/3+1/5-1/7+1/9-1/11+1/13-....)
and so on.
Here is the example of the output they want:
Pi Approximation Program
How Many terms of the series should be included?
(The more terms, the better the approximation.)
=> 3
Approximate value of pi is 3.4667
what do I have so far? a non working version of the program that my small mind can't comprehend since C is a extra terrestial language.
PHP:
#include<math.h>
//main
int main()
{
double sum;
int num;
int i;
{
printf("Pi Approximation Program\n");
printf("How Many terms of the series should be included?\n");
printf(" (The more terms, the better the approximation.)\n");
scanf("%lf", &num);
}
while(num<=0);
sum=0; //needed to sum terms
for(i=1;i<=num;i++)
{
if(i==1) //special case for first term
sum = sum+1;
if(i%2==0) //subtract even terms
sum = sum-(1/((i*2.0)-1)); //multiply by 2.0 to force real number division
//otherwise it is int/int and sum of series is
//nowhere near pi
if(i%2!=0 && i!=1) //add odd terms except for 1
sum = sum+(1/((i*2.0)-1)); //multiply by 2.0 to force real number division
//otherwise it is int/int and sum of series is
//nowhere near pi
}
printf("Pi is approximately: %f ",sum*4);
system("PAUSE");
return 0;
}
this would be a lot easier in C++ since I have a strong idea how it works.
But of course I am lost with this problem