How Can I Modify This Program to Show Complex Roots?

Click For Summary
SUMMARY

The discussion focuses on modifying a Fortran program to display both real and complex roots of a quadratic equation defined by the formula Ax² + Bx + C = 0. The original program calculates only real roots based on the discriminant (delta). To accommodate complex roots, users should declare Z1 and Z2 as complex variables and utilize the ZSQRT function for square root calculations when delta is less than zero. This adjustment allows the program to handle complex numbers effectively.

PREREQUISITES
  • Understanding of quadratic equations and the discriminant.
  • Familiarity with Fortran programming language.
  • Knowledge of complex number operations in programming.
  • Experience with Fortran's intrinsic functions, specifically ZSQRT.
NEXT STEPS
  • Learn about Fortran complex data types and their operations.
  • Explore the use of intrinsic functions in Fortran for mathematical computations.
  • Investigate error handling in Fortran when dealing with complex numbers.
  • Study the implementation of complex number arithmetic in other programming languages.
USEFUL FOR

This discussion is beneficial for Fortran developers, mathematicians, and students who need to implement and understand complex number calculations in programming, particularly in the context of solving quadratic equations.

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:

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K