SQUARE
!Program name: SQUARE
!Find
approximate square root
Implicit
none
Real,
allocatable :: x, a, b
allocate
(x)
allocate
(a)
allocate
(b)
Write
(*,*) 'Enter a number to find its approximate square root: '
Read
*, x
!use
the first routine with EXIT statement
a=1; b=x/a
do
a=(a+b)/2.0
b=x/a
print
*, 'New approximation of square root of', x, 'is: ', a
if ( abs(a-b) < 1e-10 ) then
exit
end if
end do
Print
*
Print
*, 'Precise square root of', x, 'is: ', sqrt(x)
deallocate
(a)
deallocate
(b)
!Use
the second routine with WHILE STATEMENT
a=1;
b=x/a
do while
( abs(a-b) > 1e-10 ) !inverted
logical expression
a=(a+b)/2.0
b=x/a
print *, 'New approximation of square root
of', x, 'is: ', a
end do
Print
*
Print
*, 'Precise square root of', x, 'is: ', sqrt(x)
Comentários
Postar um comentário