Solve Quadratic Equation: Difference between revisions

From TÜ Narva Kolledži Wiki
Jump to navigation Jump to search
Content deleted Content added
Created page with "<pre> # Solve the quadratic equation ax**2 + bx + c = 0 # import complex math module import math #ax2 + bx + c = 0, where #a, b and c are real numbers and #a ≠ 0 #the di..."
 
No edit summary
 
Line 36: Line 36:
print('The solution are {0} and {1}'.format(sol1,sol2))
print('The solution are {0} and {1}'.format(sol1,sol2))


if d ==0 :
if d == 0 :
sol1 = (-b)/(2*a)
sol1 = (-b)/(2*a)
print('The solution has the only root {0}'.format(sol1))
print('The solution has the only root {0}'.format(sol1))

Latest revision as of 10:05, 14 July 2026


# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module
import math

#ax2 + bx + c = 0, where
#a, b and c are real numbers and
#a ≠ 0

#the discriminant
#d = (b**2) - (4*a*c)

#(-b ± (d) ** 0.5) / (2 * a)

print ('Solve the quadratic equation ax**2 + bx + c = 0')

a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))


# calculate the discriminant
d = (b**2) - (4*a*c)
#где d = b2 - 4ac — дискриминант многочлена ax2 + bx + c.
#Если D > 0, то уравнение имеет два различных вещественных корня.
#например при a = 1, b = 1 и c = –6, корни уравнения будут -3 и 2
#Если D = 0, то оба корня равны. Например: a = 1, b = −6, c = 9;
#Если D < 0, то решений нет. Например: a = 1, b = 1, c = 6;

if d > 0 :
    # find two solutions
    sol1 = (-b-math.sqrt(d))/(2*a)
    sol2 = (-b+math.sqrt(d))/(2*a)
    print('The solution are {0} and {1}'.format(sol1,sol2))

if d == 0 :
    sol1 = (-b)/(2*a)
    print('The solution has the only root {0}'.format(sol1))

if d < 0 :
    print('There are no solutions!')