Lines 51
##### Keywords
mathematics (1) quadratic equation solver (1)
##### Permissions
Owner: Stou S.
Viewable by Everyone
Editable by All Siafoo Users

# Simple Quadratic Equation Solver 0

 In Brief An example Fortran program that given the coefficients, a, b and c of a quadratic equation , will solve for the roots using the well known quadratic formula
 Language Fortran
# 's
` 1! --------------------------------------------------- 2! Author:   3!  Dr. Ching-Kuang Shene 4!  Department of Computer Science 5!  Michigan Technological University 6! 7!   Solve  Ax^2 + Bx + C = 0 given B*B-4*A*C >= 0 8!   Now, we are able to detect the following: 9!    (1) unsolvable equation10!    (2) linear equation11!    (3) quadratic equation12!        (a) distinct real roots13!        (b) repeated root14!        (c) no real roots15! ---------------------------------------------------1617PROGRAM  QuadraticEquation18   IMPLICIT  NONE1920   REAL  :: a, b, c21   REAL  :: d22   REAL  :: root1, root22324!  read in the coefficients a, b and c2526   READ(*,*)  a, b, c27   WRITE(*,*) 'a = ', a28   WRITE(*,*) 'b = ', b29   WRITE(*,*) 'c = ', c30   WRITE(*,*)3132   IF (a == 0.0) THEN              ! could be a linear equation33      IF (b == 0.0) THEN           ! the input becomes c = 034         IF (c == 0.0) THEN        ! all numbers are roots35            WRITE(*,*)  'All numbers are roots'36         ELSE                      ! unsolvable37            WRITE(*,*)  'Unsolvable equation'38         END IF39      ELSE                         ! linear equation40         WRITE(*,*)  'This is linear equation, root = ', -c/b41      END IF42   ELSE                            ! ok, we have a quadratic equation43      d = b*b - 4.0*a*c44      IF (d > 0.0) THEN            ! distinct roots?45         d     = SQRT(d)46         root1 = (-b + d)/(2.0*a)  ! first root47         root2 = (-b - d)/(2.0*a)  ! second root48         WRITE(*,*)  'Roots are ', root1, ' and ', root249      ELSE IF (d == 0.0) THEN      ! repeated roots?50         WRITE(*,*)  'The repeated root is ', -b/(2.0*a)51      ELSE                         ! complex roots52         WRITE(*,*)  'There is no real roots!'53         WRITE(*,*)  'Discriminant = ', d54      END IF55   END IF5657END PROGRAM  QuadraticEquation`

An example Fortran program that given the coefficients, a, b and c of a quadratic equation , will solve for the roots using the well known quadratic formula