Showing posts with label Python Mathematics. Show all posts
Showing posts with label Python Mathematics. Show all posts

Thursday, 30 April 2026

Solve Any Quadratic Equation in Python Using User Input (Step-by-Step Guide)

 


Mathematics meets programming in one of the most practical ways—solving equations using code.

In this guide, you’ll learn how to build a Python program that takes user input and solves any quadratic equation instantly.

Let’s turn a classic math formula into real-world code ๐Ÿ‘‡


What is a Quadratic Equation?

A quadratic equation looks like this:

ax2+bx+c=0

Where:

  • a, b, c are constants
  • x is the variable we want to find

To solve it, we use the quadratic formula:

x=b±b24ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

Understanding the Discriminant

The part inside the square root is called the discriminant:

D=b24acD = b^2 - 4ac

It determines the type of roots:

  • D > 0 → Two real and distinct roots
  • D = 0 → One real root
  • D < 0 → Complex (imaginary) roots

 Python Implementation

Now let’s convert this logic into Python code that takes input from the user. 

import math # taking input a = float(input("Enter a: ")) b = float(input("Enter b: ")) c = float(input("Enter c: ")) # discriminant d = b**2 - 4*a*c # solving if d > 0: x1 = (-b + math.sqrt(d)) / (2*a) x2 = (-b - math.sqrt(d)) / (2*a) print("Two real roots:", x1, x2) elif d == 0: x = -b / (2*a) print("One real root:", x) else: real = -b / (2*a) imag = math.sqrt(-d) / (2*a) print("Complex roots:", real, "+", imag, "i and", real, "-", imag, "i")



















Example Run

Enter a: 1
Enter b: -3
Enter c: 2

Output:

Two real roots: 2.0 1.0

Key Concepts You Learned

  • Taking user input in Python
  • Using the math module
  • Applying mathematical formulas in code
  • Handling different cases (real & complex roots)

 Pro Tip

Always make sure:

  • a ≠ 0, otherwise it's not a quadratic equation
  • Use float() to handle decimal values

Conclusion

With just a few lines of Python, you can solve any quadratic equation automatically. This is a perfect beginner project that combines math + programming logic.

Once you understand this, you can extend it further:

  • Build a GUI calculator ๐Ÿ–ฅ️
  • Plot graphs of equations ๐Ÿ“Š
  • Turn it into a web app ๐ŸŒ

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (254) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (29) Azure (10) BI (10) Books (262) Bootcamp (11) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (32) Data Analytics (22) data management (15) Data Science (353) Data Strucures (17) Deep Learning (158) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (72) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (292) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (33) pytho (1) Python (1330) Python Coding Challenge (1132) Python Mathematics (1) Python Mistakes (51) Python Quiz (491) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (49) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)