atilarceus
Programmer
Hey guys, i were writing a code to calculate the digits of pi using the leibniz formula; the code is shown below:
The thing is, the code can be compiled, but the 'g' expression inside the loop gives 1 in the first iteration, when it should be 0,6666666..., and zero in every other iteration (what clearly is wrong), not giving the value of pi. I want to know what i'm doing wrong and how can i fix it. Thanks guys!
Obs.:forgive my english, i dunno if i wrote something wrong, i really didn't check it in a translator.
Code:
program picalc
implicit none
real :: pi,g
integer :: i,l
pi=0
print *, "Tell me the precision you want"
read(*,*) l
do i=0,l
g = (1/(4*i+1))-(1/(4*i+3))
pi=pi+g
end do
print *, 'The calculated value of pi is', 4*pi
end program picalc
Obs.:forgive my english, i dunno if i wrote something wrong, i really didn't check it in a translator.