TUTORIAL 2
Question 1 : Qawiem – 2024218714
(Algebra: solve quadratic equations) The two roots of a quadratic equation, for example,
𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0 , can be obtained using the following formula:
−𝑏 + √𝑏2 − 4𝑎𝑐
𝑟1 =
2𝑎
and
−𝑏 − √𝑏2 − 4𝑎𝑐
𝑟1 =
2𝑎
𝑏2 − 4𝑎𝑐 is called the discriminant of the quadratic equation. If it is positive, the equation has two
real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a program that prompts the user to enter values for a, b, and c and displays the result based on
the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one
root. Otherwise, display The equation has no real roots.
So,
Discriminant = 𝑏2−4ac
Discriminant value What it means? Result
> 0 (positive) Square root of a positive number is real Two real roots
=0 Square root of zero is zero One real root (both same)
< 0 (negative) Square root of negative number is not real No real roots (only complex roots)
CODE
# Program to solve quadratic equation ax² + bx + c = 0
import math # Import the math module so we can use [Link]() to find square roots
# Get user input for coefficients a, b, and c (convert to float so we can handle decimals)
a = float(input("Enter value for a: ")) # Coefficient of x²
b = float(input("Enter value for b: ")) # Coefficient of x
c = float(input("Enter value for c: ")) # Constant term
# Step 1: Calculate the discriminant using the formula (b² - 4ac)
# This value determines how many real roots the equation has.
discriminant = b**2 - 4*a*c
# Step 2: Use conditional statements to check the type of roots
if discriminant > 0:
# If discriminant is positive (> 0), there are TWO distinct real roots.
# Step 3: Calculate both roots using the quadratic formula:
# r₁ = (-b + √(b² - 4ac)) / (2a)
# r₂ = (-b - √(b² - 4ac)) / (2a)
r1 = (-b + [Link](discriminant)) / (2*a)
r2 = (-b - [Link](discriminant)) / (2*a)
# Step 4: Display both roots
print("The equation has two real roots:", r1, "and", r2)
elif discriminant == 0:
# If discriminant is exactly 0, there is ONE real root (both roots are the same).
# Step 3: Use the simplified formula:
# r = -b / (2a)
# (since √0 = 0, both + and - give the same result)
r = -b / (2*a)
# Step 4: Display the single root
print("The equation has one root:", r)
else:
# If discriminant is negative (< 0), then the square root of a negative number
# is not real (it's imaginary). So the equation has NO real roots.
# Python’s [Link]() cannot handle negative numbers (it would cause an error),
# so we just print a message.
print("The equation has no real roots.")
Result and Discussion
1. Discriminant > 0 → Two real roots
Equation:
𝒙𝟐 − 𝟓𝒙 + 𝟔 = 𝟎
Values:
a = 1, b = -5, c = 6
Calculate discriminant:
𝑏2 − 4𝑎𝑐 = (−5)2 − 4(1)(6) = 25 − 24 = 1
2. Discriminant = 0 → One real root
Equation:
𝒙𝟐 + 𝟒𝒙 + 𝟒 = 𝟎
Values:
a = 1, b = 4, c = 4
Calculate discriminant:
𝑏2 − 4𝑎𝑐 = (4)2 − 4(1)(4) = 16 − 16 = 0
3. Discriminant < 0 → No real roots
Equation:
𝟐𝒙𝟐 + 𝟐𝒙 + 𝟒 = 𝟎
Values:
a = 2, b = 2, c = 4
Calculate discriminant:
𝑏2 − 4𝑎𝑐 = (2)2 − 4(2)(4) = 4 − 32 = −28