How Can I Modify This Program to Show Complex Roots?

In summary, the program delta1 is a Fortran program that calculates the roots of a quadratic equation given the values of A, B, and C. It first calculates the value of delta and then checks if it is positive, zero, or negative. Based on the value of delta, it determines whether the roots are real or complex and calculates the corresponding values. If delta is positive, the program outputs two real roots. If delta is zero, there is only one real root. If delta is negative, the program outputs two complex roots. It uses the ZSQRT function to calculate the square root of a complex number.
  • #1
surfer
6
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
  • #2
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:

1. What is FORTRAN?

FORTRAN (Formula Translation) is a high-level programming language used for scientific and engineering applications. It was first developed in the 1950s and is still commonly used today.

2. What is the quadratic equation?

The quadratic equation is a mathematical formula used to solve quadratic equations of the form ax^2 + bx + c = 0. It is commonly used in algebra and is used to find the roots or solutions of a quadratic equation.

3. How do I code a quadratic equation using FORTRAN?

To code a quadratic equation using FORTRAN, you can use the built-in functions SQRT and POWER to calculate the square root and power of a number. You will also need to define the coefficients a, b, and c and use the quadratic formula to calculate the roots.

4. Can FORTRAN solve complex quadratic equations?

Yes, FORTRAN can solve complex quadratic equations. You can use the built-in functions CMPLX and CONJG to handle complex numbers and solve the equation using the quadratic formula.

5. Are there any limitations to solving quadratic equations using FORTRAN?

One limitation of using FORTRAN to solve quadratic equations is that it can only handle single-variable equations. If you need to solve a system of equations or equations with multiple variables, you will need to use a different programming language or algorithm.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
589
  • Programming and Computer Science
Replies
12
Views
953
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
30
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
876
Back
Top