Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
How to Solve Quadratic Equation in Python? (Program Examples)
Quadratic equations have their applications in various fields, from mathematics and physics to computer science and engineering. Understanding how to solve quadratic equation in Python not only enhances our problem-solving skills but also equips us with a versatile tool to tackle real-world challenges.
Whether you're a math enthusiast, a student, or a Python programmer, learning the Python program to solve quadratic equations opens up a realm of possibilities. In this digital era, where data-driven decision-making is vital, the ability to analyze and interpret quadratic equations becomes increasingly important.
The standard form of a quadratic equation is:
ax^2 + bx + c = 0
In this equation, "a," "b," and "c" are coefficients, and "x" is the variable.
Here are a few examples of quadratic equations in standard form:
x^2 - 5x + 6 = 0
2x^2 + 3x - 2 = 0
-4x^2 + 2x + 1 = 0
Here, we will explore different techniques and programs, and use Python to solve quadratic equations efficiently.
Whether you're modeling the trajectory of a projectile, analyzing financial data, optimizing processes, or designing algorithms, the ability to solve quadratic equations empowers you to make accurate predictions, uncover hidden patterns, and make informed decisions.
So, let’s get started and know the quadratic equation program in Python.
Python Program to Solve Quadratic Equation
Code
# Simple quadratic equation program in Python
import cmath
# Input coefficients from the user
a = float(input("Enter the coefficient of x^2: "))
b = float(input("Enter the coefficient of x: "))
c = float(input("Enter the constant term: "))
# Calculate the discriminant
discriminant = (b ** 2) - (4 * a * c)
# Calculate the solutions
solution1 = (-b + cmath.sqrt(discriminant)) / (2 * a)
solution2 = (-b - cmath.sqrt(discriminant)) / (2 * a)
# Print the solutions
print("The solutions are:")
print("Solution 1:", solution1)
print("Solution 2:", solution2)
Output
Enter the coefficient of x^2: 3
Enter the coefficient of x: 3
Enter the constant term: 4
The solutions are:
Solution 1: (-0.5+1.0408329997330663j)
Solution 2: (-0.5-1.0408329997330663j)
Explanation
-
We first import the cmath module to handle complex numbers, as the solutions to a quadratic equation may involve complex numbers.
-
Next, we take input from the user for the coefficients of the quadratic equation (a, b, c).
-
Then, we calculate the discriminant using the formula: discriminant = b^2 - 4ac.
-
Next, we calculate the two solutions using the quadratic formula: (-b ± √(discriminant)) / (2a).
-
Finally, we print the solutions of the quadratic equation in Python.
Quadratic Equation in Python Using Class
Code
# Solve quadratic equation in Python with class
import cmath
class QuadraticEquation:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def calculate_discriminant(self):
return (self.b ** 2) - (4 * self.a * self.c)
def calculate_solutions(self):
discriminant = self.calculate_discriminant()
solution1 = (-self.b + cmath.sqrt(discriminant)) / (2 * self.a)
solution2 = (-self.b - cmath.sqrt(discriminant)) / (2 * self.a)
return solution1, solution2
# Input coefficients from the user
a = float(input("Enter the coefficient of x^2: "))
b = float(input("Enter the coefficient of x: "))
c = float(input("Enter the constant term: "))
# Create an instance of the QuadraticEquation class
equation = QuadraticEquation(a, b, c)
# Calculate and print the solutions of the quadratic equation
solutions = equation.calculate_solutions()
print("The solutions are:")
print("Solution 1:", solutions[0])
print("Solution 2:", solutions[1])
Output
Enter the coefficient of x^2: 1
Enter the coefficient of x: 5
Enter the constant term: 6
The solutions are:
Solution 1: (-2+0j)
Solution 2: (-3+0j)
Explanation
-
We define a class called QuadraticEquation. The constructor (__init__ method) initializes the coefficients of the quadratic equation (a, b, c) as instance variables.
-
We also define a method called calculate_discriminant within the class, which calculates the discriminant of the quadratic equation using the formula: discriminant = b^2 - 4ac.
-
The calculate_solutions method calculates the solutions of the quadratic equation using the quadratic formula: (-b ± √(discriminant)) / (2a).
-
It first calls the calculate_discriminant method, then uses the discriminant to calculate the solutions.
-
We then take input from the user for the coefficients of the quadratic equation (a, b, c).
-
Next, we create an instance of the QuadraticEquation class, passing in the user-provided coefficients as arguments to the constructor.
-
Finally, we call the calculate_solutions method on the equation object to calculate the solutions of the quadratic equation, and we print the results.
Using a class provides a structured and object-oriented approach to solve the quadratic equation problem, encapsulating the coefficients and calculation methods within a single entity.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Factorial Program
- Python Program to Find Area of Triangle
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program