Skip to content

Commit 8c58e20

Browse files
Update readme.md
Co-Authored-By: Antim-IWP <[email protected]>
1 parent 36a1ec3 commit 8c58e20

File tree

1 file changed

+95
-0
lines changed
  • Interview/Python/Solutions/140+ Basic Python Programs/Program 9

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 📐 Quadratic Equation Solver (Python)
2+
3+
This Python program solves **quadratic equations** of the form:
4+
5+
```
6+
ax² + bx + c = 0
7+
```
8+
9+
It uses the quadratic formula to compute the roots.
10+
11+
---
12+
13+
## 📌 Problem Statement
14+
15+
**Write a Python program to solve a quadratic equation.**
16+
17+
---
18+
19+
## 🧮 Quadratic Formula
20+
21+
```
22+
x = (-b ± √(b² - 4ac)) / 2a
23+
```
24+
25+
The value inside the square root, `b² - 4ac`, is called the **discriminant (D)**:
26+
- If D > 0: Two real and distinct roots
27+
- If D = 0: One real root (repeated)
28+
- If D < 0: Two complex roots
29+
30+
---
31+
32+
## ✅ Sample Code
33+
34+
```python
35+
import cmath # For complex number support
36+
37+
# Get coefficients from user
38+
a = float(input("Enter coefficient a: "))
39+
b = float(input("Enter coefficient b: "))
40+
c = float(input("Enter coefficient c: "))
41+
42+
# Calculate the discriminant
43+
d = (b**2) - (4*a*c)
44+
45+
# Calculate the roots
46+
root1 = (-b + cmath.sqrt(d)) / (2*a)
47+
root2 = (-b - cmath.sqrt(d)) / (2*a)
48+
49+
# Display the result
50+
print(f"The roots of the equation are {root1} and {root2}")
51+
```
52+
53+
---
54+
55+
## ▶️ Example Run
56+
57+
```bash
58+
Enter coefficient a: 1
59+
Enter coefficient b: -3
60+
Enter coefficient c: 2
61+
The roots of the equation are (2+0j) and (1+0j)
62+
```
63+
64+
---
65+
66+
## 🚀 How to Run
67+
68+
1. Save the code in a file named `quadratic_solver.py`
69+
2. Run the script using:
70+
```bash
71+
python quadratic_solver.py
72+
```
73+
74+
---
75+
76+
## 📁 Suggested Project Structure
77+
78+
```
79+
quadratic_solver/
80+
├── quadratic_solver.py
81+
└── README.md
82+
```
83+
84+
---
85+
86+
## 💡 You Can Try
87+
88+
- Add input validation for `a != 0`.
89+
- Format roots to hide the complex notation when not needed.
90+
- Create a GUI using Tkinter.
91+
- Add a plot of the quadratic curve using `matplotlib`.
92+
93+
---
94+
95+
Happy Solving! 🧮✨

0 commit comments

Comments
 (0)