Bifalgorithm post · Computer science one · Computing Quadratic Roots

How does PHP compute the roots of a quadratic equation?

How does PHP compute the roots of a quadratic equation? (Computer science one · Computing Quadratic Roots) — "We need to take care that we correctly adapt the…

Ideas in this flowchart

  • Programa PHP para calcular las raíces de una ecuación cuadrática
  • Solicita y convierte los coeficientes a, b y c
  • Introduce el coeficiente a
  • Introduce el coeficiente b
  • Introduce el coeficiente c
  • Adapta la fórmula cuadrática para reflejar correctamente el orden de las operaciones
  • Utiliza la función sqrt() de la biblioteca matemática
  • Calcula root1 con la suma de la raíz cuadrada
  • Calcula root2 con la resta de la raíz cuadrada
  • Imprime los coeficientes y las dos raíces mediante printf()
  • El programa completo aparece en el ejemplo de código 37.5
Feed/

How does PHP compute the roots of a quadratic equation?

No cover

Channel

Computer science one

1 page

"We need to take care that we correctly adapt the formula so it accurately reflects the order of operations. We also need to use the math library’s square root function.\n\n$root1 = (-$b + sqrt($b*$b - 4*$a*$c) ) / (2*$a);\n\n$root2 = (-$b - sqrt($b*$b - 4*$a*$c) ) / (2*$a);\n\nFinally, we print the output using printf(). The full program can be found in Code Sample 37.5.\n\n<?php\n\n/**\n\n* This program computes the roots to a quadratic equation\n\n* using the quadratic formula.\n\n*/\n\nprintf(\"Please enter a: \");\n\n$a = floatval(trim(fgets(STDIN)));\n\nprintf(\"Please enter b: \");\n\n$b = floatval(trim(fgets(STDIN)));\n\nprintf(\"Please enter c: \");\n\n$c = floatval(trim(fgets(STDIN)));\n\n$root1 = (-$b + sqrt($b*$b - 4*$a*$c) ) / (2*$a);\n\n$root2 = (-$b - sqrt($b*$b - 4*$a*$c) ) / (2*$a);\n\nprintf(\"The roots of %fx^2 + %fx + %f are:\n\n\", $a, $b, $c);\n\nprintf(\" root1 = %f\n\n\", $root1);\n\nprintf(\" root2 = %f\n\n\", $root2);\n\n?>\n\nCode Sample 37.5.: Quadratic Roots Program in PHP"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.