Quadratic Equation (Real & Complex Roots) | FORTRAN 95

Quadratic Equation (Real & Complex Roots) | FORTRAN 95

PROGRAM quadratic_eq
IMPLICIT NONE

!declaring variables

REAL::a,b,c,d,root1,root2,val

PRINT *, '============================================'
PRINT *, 'PROGRAM TO CALCULATE ROOTS OF A QUADRATIC EQUATION [REAL & COMPLEX BOTH] [ BY WWW.BOTTOMSCIENCE.COM ]'
PRINT *, '============================================'

PRINT *,'Please Enter the Value of Cofficients a,b and c Respectively as Per the Equation - ax^2 + bx + c'

READ(*,*)a,b,c

! calculating the root part

val=((b**2)-(4*a*c))

!checking condition

IF (val>0 .OR. val==0) then

d=SQRT(val)

root1=(-b/(2*a))+(d/(2*a))
root2=(-b/(2*a))-(d/(2*a))

PRINT *,'Calculated Real Roots are: Root 1 = ', root1 ,'Root 2 = ', root2

ELSE

val=-val

d=SQRT(val)

PRINT *,'Calculated Complex Roots Are:'
PRINT *, 'Root 1 = ', (-b/(2.*a)) ,'i','(',+(d/(2*a)),')' 
PRINT *, 'Root 2 = ', (-b/(2.*a)) ,'i','(',-(d/(2*a)),')'

END IF

END PROGRAM

OUTPUT

OUTPUT - QUADRATIC EQUATION [REAL & COMPLEX ROOTS]
OUTPUT – QUADRATIC EQUATION [REAL & COMPLEX ROOTS]

Leave a Reply

Your email address will not be published.