Fortran How Can I Modify This Program to Show Complex Roots?

AI Thread Summary
The discussion focuses on modifying a Fortran program to display both real and complex roots of a quadratic equation. The original program calculates real roots when the discriminant (delta) is non-negative but does not handle complex roots for negative delta values. To address this, participants suggest declaring Z1 and Z2 as complex variables and using the ZSQRT function for calculations when delta is negative. This adjustment allows the program to output complex roots correctly. Overall, the modification enhances the program's capability to handle all possible root scenarios for the quadratic equation.
surfer
Messages
6
Reaction score
0
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?
 
Technology news on Phys.org
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
5
Views
3K
Replies
6
Views
2K
Replies
12
Views
1K
Replies
19
Views
2K
Replies
13
Views
2K
Replies
11
Views
2K
Replies
3
Views
3K
Back
Top