Click to See Complete Forum and Search --> : How could I write this?


rascal22
12-01-2003, 07:21 PM
I am taking a class and just begining to learn how to write scripts. How would I write Pi/4=1-(1/3)+(1/5)-(1/7)+(1/9)...
The user inputs how many times it loops, and it can be a for loop or a while. I know I need a sum that starts at 0, and a
controled counter starting at 1. But, how would I get the program to steadily increase by 2 in the denominator, and then
subtract one time, and then add the next? Thanks for your time

skriptor
12-02-2003, 12:45 AM
Hi,
think about little steps:
i = 1;
factor = 1;
sum = 0;

j = 1 / i;
sum = sum + ( factor * j );
factor = factor * (-1);
i = i + 2;


Good luck, skriptor