{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bonus: Quadratic Equation\n",
"A [quadratic equation](https://en.wikipedia.org/wiki/Quadratic_equation) is an equation that can be written as\n",
"\n",
"$ax^2 + bx + c = 0$.\n",
"\n",
"In this equation *x* represents an unknown number, and *a*, *b*, and *c* are representing known numbers.\n",
"Possible solutions for a given quadratic equation can be calculated by the formula\n",
"\n",
"$x=\\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}$.\n",
"\n",
"The expression $b^2-4ac$ is called the\n",
"[discriminant](https://en.wikipedia.org/wiki/Quadratic_equation#Discriminant).\n",
"Using the discriminant makes it is easy to check the number of solutions for a given quadratic equation:\n",
"\n",
"- If the discriminant is 0, the quadratic equation has exactly one real solution.\n",
"- If the discriminant is > 0, the quadratic equation has two real solutions.\n",
"- If the discriminant is < 0, the quadratic equation has two complex solutions.\n",
"\n",
"Write a program that asks the user for the numbers *a*, *b* and *c*.\n",
"The program should then print out how many solutions the quadratic equation has.\n",
"\n",
"Below is an example execution of the program:\n",
"\n",
" Please enter the value of a: 4\n",
" Please enter the value of b: 2\n",
" Please enter the value of c: -2\n",
"\n",
" The quadratic equation has 2 real solutions.\n",
"\n",
"\n",
"Here is an another example execution of the program:\n",
"\n",
" Please enter the value of a: 1\n",
" Please enter the value of b: 2\n",
" Please enter the value of c: 3\n",
"\n",
" The quadratic equation has 2 complex solutions.\n",
"\n",
"\n",
"And here is yet another example execution of the program:\n",
"\n",
" Please enter the value of a: 1\n",
" Please enter the value of b: 2\n",
" Please enter the value of c: 1\n",
"\n",
" The quadratic equation has 1 real solution."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = int(input(\"Please enter the value of a: \"))\n",
"b = int(input(\"Please enter the value of b: \"))\n",
"c = int(input(\"Please enter the value of c: \"))\n",
"\n",
"discriminat = (b * b) - (4 * a * c)\n",
"\n",
"if discriminat < 0:\n",
" print(\"The quadratic equation has 2 complex solutions.\")\n",
"elif discriminat == 0:\n",
" print(\"The quadratic equation has 1 real solution.\")\n",
"else:\n",
" print(\"The quadratic equation has 2 real solutions.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}