C This program prompts the user for a real number, N, and then C calculates the square root of that number approximately by finding C the root of the equation x**2 - N. The root is found using the C bisection technique. C C To ensure the root is bracketed, the initialize the interval lower C and upper bounds to be unity and N, respectively, if the number is C greater than one and values of N and unity, respectively, if the C number is less than one. Bisectioning terminates and the root is C considered found when, for an interval mid-point c, |f(c,N)| <= C |c/10^5|. The program reports the number of iterations where were C required to find the root. C C The user terminates input with control-D at which point the C program reports the number of values that were entered. C C John Schneider C CptS 203, HW 6 C-------------------------------------------------------------------------- program sqroot real a, b, mid, num integer count, count1 C..... C a = lower limit of interval C b = upper limit of interval C mid = mid point of interval C num = number for which the square root is to be calculated C C count = count of the number of iterations needed to find root C count1 = count of the number of values entered C..... count1 = 0 10 continue print *, 'Enter a positive number.' read (5,*,end=30) num count1 = count1 + 1 C........ C Initialize upper and lowever limits of interval that bracket C root. C........ if (num .gt. 1.0) then a = 1.0 b = num else a = num b = 1.0 endif mid = (a+b)/2.0 count = 0 C........While stopping condition is not met, continue bisectioning. 20 if (abs(f(mid,num)) .gt. abs(mid*1.e-5)) then if (f(a,num)*f(mid,num) .lt. 0.0) then b=mid else a=mid endif mid = (a+b)/2.0 count = count + 1 goto 20 endif print *, 'Square root of number is ',mid,'.' print *, 'It took ',count,' iterations to get there.' print * goto 10 30 continue print *, 'You entered a total of ',count1,' numbers. Bye!' stop end C..... C Function for which we are finding the root. C..... real function f(x,num) real x, num f = x*x - num C print *, 'f = ',f return end