-2

(-12+√(b^2-4ac))/2a

(where a = 1, b = 5, c = 3)

How do I write a code for a quadratic equation?

  • Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – sushanth Jan 05 '22 at 03:14

2 Answers2

0

Okay, we can have three notions in here:

  1. We have 3 numbers (a, b, and c), which will be used in the equation because that keeps them in a variable with a scope greater than the equations we will make next.
  2. We have two different equations the first one that will be
    • Δ = b^2 - 4ac
    • x = (-b ± √Δ)/2a
  3. And the most important one, in the quadratic equation we have one root if Δ = 0, two if Δ > 0 or is not possible to reach the root if Δ < 0, in that case, will be sent an error message.

With all that in mind, I can show an example in a simple language is python how works in practice:

import cmath

a = 1
b = 5
c = 3

delta = (b**2) - (4*a*c)

root1 = (-b-cmath.sqrt(delta))/(2*a)
root2 = (-b+cmath.sqrt(delta))/(2*a)

print('The solution are {0} and {1}'.format(root1 ,root2))

the result will be

The solution are (-4.302775637731995) and (-0.6972243622680054)

-1

In python

a=1
b=5
c=3
value = (-12+(b**2-4*a*c)**(1/2))/(2*a)
print(value)

This outputs the answer as -4.197224362268005

Binabh Devkota
  • 339
  • 3
  • 4