Fortran FORTRAN: Quadratic equation

  • Thread starter surfer
  • Start date
Ax^2+Bx+C=0



PROGRAM delta1
IMPLICIT NONE
REAL :: A,B,C
REAL :: DELTA,X1,X2
PRINT *,"Enter the a,b,and c"
READ *,A,B,C
delta=b**2-4*a*c

IF (delta>0)THEN
PRINT *,"The roots are real"
X1=(-B+SQRT(DELTA))/(2*A)
X2=(-B-SQRT(DELTA))/(2*A)
PRINT *,"X1 = ",X1,"X2 = ",X2
END IF
IF (delta==0)THEN
PRINT *,"There is one real root"
X1 = -B/(2*A)
PRINT *,"X = ",X1
END IF
IF (delta<0)THEN
PRINT *,"The roots are complex"
X1=(-B+SQRT(DELTA))/(2*A)
X2=(-B-SQRT(DELTA))/(2*A)
END IF
END PROGRAM delta1

How can i show the complex and real roots in this program above which shows only real roots?
 

DrClaude

Mentor
6,807
2,923
Fortran handles complex numbers quite easily. Declare Z1 and Z2 as complex and change the code to
Fortran:
IF (delta<0)THEN
PRINT *,"The roots are complex"
Z1=(-B+ZSQRT(DELTA))/(2*A)
Z2=(-B-ZSQRT(DELTA))/(2*A)
END IF
 
Last edited by a moderator:

Want to reply to this thread?

"FORTRAN: Quadratic equation" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top