---
title: "Quadratic equation roots in FORTRAN"
subject: "Computer Programming"
chapter: 11
key: "semester_1/computer_programming:fortran-quadratic-roots"
source: https://www.bctengineer.com/q/semester_1/computer_programming/fortran-quadratic-roots
site: BCT Engineering
---

# Quadratic equation roots in FORTRAN

Computer Programming · Chapter 11: Programming Language: FORTRAN

- Asked in: 2071 Chaitra, 8 marks; 2069 Asar, 8 marks (asked 2 times)
- Video: [003: PROGRAM IN FORTRAN TO FIND ROOTS OF QUADRATIC EQUATION](https://www.youtube.com/watch?v=CSTp1_DvrS4) — adventure in mathematics
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/fortran-quadratic-roots
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-11

## Question

Write a FORTRAN program to display nature of roots of a quadratic equation. Calculate and display the roots, if they are real and equal. (Solve quadratic equation and display roots in proper format.)

## Short answer

Read coefficients a, b, c. If a=0, output 'not quadratic'. Compute discriminant D=b²-4ac. For D>0, real distinct roots (-b±√D)/(2a); D=0, real equal root -b/(2a); D<0, complex roots with real part -b/(2a) and imaginary part √(-D)/(2a).

## Answer

**Method.** For a x² + b x + c = 0, the discriminant is D = b² − 4ac.
- D > 0: two real, distinct roots (−b ± √D) / 2a.
- D = 0: real and equal roots, x = −b / 2a.
- D < 0: complex roots, −b/2a ± i √(−D)/2a.
- a = 0: not a quadratic equation.

```fortran
      PROGRAM QUAD
      REAL A, B, C, D, X1, X2, RP, IP
      PRINT *, 'ENTER A, B, C'
      READ *, A, B, C
      IF (A .EQ. 0.0) THEN
         PRINT *, 'NOT A QUADRATIC EQUATION'
         STOP
      END IF
      D = B*B - 4.0*A*C
      IF (D .GT. 0.0) THEN
         PRINT *, 'ROOTS ARE REAL AND DISTINCT'
         X1 = (-B + SQRT(D)) / (2.0*A)
         X2 = (-B - SQRT(D)) / (2.0*A)
         WRITE(*,10) X1, X2
      ELSE IF (D .EQ. 0.0) THEN
         PRINT *, 'ROOTS ARE REAL AND EQUAL'
         X1 = -B / (2.0*A)
         WRITE(*,20) X1
      ELSE
         PRINT *, 'ROOTS ARE COMPLEX'
         RP = -B / (2.0*A)
         IP = SQRT(-D) / ABS(2.0*A)
         WRITE(*,30) RP, IP, RP, IP
      END IF
   10 FORMAT(' X1 = ', F8.3, '   X2 = ', F8.3)
   20 FORMAT(' X1 = X2 = ', F8.3)
   30 FORMAT(' X = ', F8.3, ' + ', F8.3, 'I  AND  ',
     1       F8.3, ' - ', F8.3, 'I')
      STOP
      END
```

**Sample results (worked by hand)**
- a, b, c = 1, −5, 6: D = 1, roots `X1 = 3.000   X2 = 2.000`.
- a, b, c = 1, −6, 9: D = 0, roots `X1 = X2 = 3.000`.
- a, b, c = 1, 2, 5: D = −16, roots −1.000 ± 2.000 i.

The format codes `F8.3` print a real in 8 columns with 3 digits after the decimal point.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is the nature of roots decided?
   A. From D = b^2 - 4ac
   B. From D = b^2 + 4ac
   C. From D = 2a - b
   D. From c alone
   Answer: A. The discriminant D = b^2 - 4ac decides if roots are distinct real, equal real or complex. (Key point: Discriminant)

2. What are the roots when D = 0?
   A. Two distinct real roots
   B. Complex roots
   C. Real and equal roots x = -b/(2a)
   D. No roots
   Answer: C. When D = 0 the square-root term vanishes, leaving a repeated root x = -b/(2a). (Key point: Equal roots)

3. Which D gives complex roots, and how is the imaginary part found in the program?
   A. D > 0; SQRT(D)/(2a)
   B. D < 0; SQRT(-D)/ABS(2a)
   C. D = 0; -b/(2a)
   D. D < 0; SQRT(D) directly
   Answer: B. A negative D gives complex roots -b/2a +- i sqrt(-D)/2a, so the program takes the square root of -D. (Key point: Complex roots)

4. Why does the program test a = 0 first?
   A. Because the equation is then not quadratic and 2a would be a division by zero
   B. To print the answer in F8.3
   C. To make D positive
   D. To speed up sorting
   Answer: A. If a = 0 the equation is linear and the formula would divide by 2a = 0. (Key point: Case a = 0)

## Related in Computer Programming

- [Roots of quadratic equation in C](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aquadratic-roots-c) — Computer Programming, chapter 5
- [Algorithm and flowchart: quadratic roots](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgo-flowchart-quadratic) — Computer Programming, chapter 2
- [Structure of a FORTRAN program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-structure) — Computer Programming, chapter 11, asked 5×
- [Palindrome check in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-palindrome) — Computer Programming, chapter 11
- [Sort array ascending in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-sort-ascending) — Computer Programming, chapter 11, asked 2×
- [Largest of n numbers in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-largest-n-numbers) — Computer Programming, chapter 11

## Related in other subjects

- [Test cases for a quadratic equation program](https://www.bctengineer.com/question.md?key=semester_5%2Fsoftware_engineering%3Aquadratic-test-cases) — Software Engineering, chapter 8
- [Bernoulli's equation in real practice](https://www.bctengineer.com/question.md?key=semester_6%2Fhydro_power%3Abernoulli-real-practice) — Hydro Power, chapter 1
- [Derive 1D wave equation and solve](https://www.bctengineer.com/question.md?key=semester_4%2Fapplied_mathematics%3Awave-eq-derive-and-solve) — Applied Mathematics, chapter 3, asked 5×
- [Derive 1D heat equation and solve](https://www.bctengineer.com/question.md?key=semester_4%2Fapplied_mathematics%3Aheat-eq-derive-and-solve) — Applied Mathematics, chapter 3, asked 4×
