# Field Manual for Hackers

## PHP - Write a program to find all the types of roots of a given quadratic equation.

#### 4 comments:

1. ";
echo "Coefficient of b is: ".\$coff_b;
echo "
";
echo "Coefficient of c is: ".\$coff_c;
echo "
";
\$discriminant=\$b*\$b-4*\$a*\$c;

if(\$discriminant>0)
{
echo "Roots are real and different";
echo "
";
\$x1=(-coff_b+sqrt(\$discriminant))/2*coff_a;
\$x2=(-b-sqrt(\$discriminant))/2*a;
echo " The roots of the quadratic equations are \$x1 and \$x2";
echo "
";
}

elseif(\$discriminant==0)
{
echo "Roots are real and same";
echo "
";
\$x1=(-b+sqrt(\$discriminant))/2*a;
\$x2=\$x1;
echo " The roots of the quadratic equations are \$x1 and \$x2";
echo "
";
}

else
{
echo "Roots are imaginary and different";
echo "
";
\$rp=-b/2*a;
\$img=sqrt(-\$discriminant)/2*a;
echo " The roots of the quadratic equations are x1=\$rp+\$img and x2=\$rp-\$img";
echo "
";
}

?>

2. \$a=\$_POST["num"];
\$b=\$_POST["num2"];
\$c=\$_POST["num3"];
\$determinant=pow(\$b,2)-4*\$a*\$c;
\$i=-1;
if(\$determinant>=0)
{
echo ((-\$b-sqrt(\$determinant))/(2*\$a));
echo "
";
echo ((-\$b+sqrt(\$determinant))/(2*\$a));
}
else
{echo "Non real roots
";
echo ((-\$b/(2*\$a))-(sqrt(\$determinant*\$i)/2*\$a));
echo "
";
echo ((-\$b/(2*\$a))+(sqrt(\$determinant*\$i)/2*\$a));
}

3. php
\$b=\$_POST['n1'];
\$a=\$_POST['n2'];
\$c=\$_POST['n3'];
\$d=\$b*\$b-4*\$a*\$c;
if(\$d>0)
{\$root1=(-\$b+sqrt(\$d))/(2*\$a);
\$root2=(-\$b-sqrt(\$d))/(2*\$a);
echo "roots are \$root1 and \$root2 which are real";
}
else if(\$d==0)
{\$root1=-\$b/(2*a);
\$root2= \$b/(2*a);
echo "roots are \$root1 and \$root2 which are real and equal";
}
else if(\$d<0)
{echo "roots are imaginary";
}

4. \$a=\$_POST["num"];
\$b=\$_POST["num2"];
\$c=\$_POST["num3"];
\$determinant=pow(\$b,2)-4*\$a*\$c;
\$i=-1;
if(\$determinant>=0)
{
echo ((-\$b-sqrt(\$determinant))/(2*\$a));
echo "
";
echo ((-\$b+sqrt(\$determinant))/(2*\$a));
}
else
{echo "Non real roots
";
echo ((-\$b/(2*\$a))-(sqrt(\$determinant*\$i)/2*\$a));
echo "
";
echo ((-\$b/(2*\$a))+(sqrt(\$determinant*\$i)/2*\$a));
}