Python Program to find roots of a Quadratic Equation
top of page

Python Program to find roots of a Quadratic Equation

Updated: May 30, 2021


There are two method for finding roots of quadratic equation:

Program: First Method:

import cmath

a = float (input("Enter the value of a : ")) b = float (input("Enter the value of b : ")) c = float (input("Enter the value of c : "))

dis = (b * b) - (4 * a * c)

r1=(-b + cmath.sqrt(dis)) / (2 * a) r2=(-b - cmath.sqrt(dis)) / (2 * a)

print(" The square roots of given equation are {0} and {1}".format(r1,r2))

Output:

Enter the value of a : 1.2

Enter the value of b : 2.5

Enter the value of c : 3.5

The square roots of given equation are (-1.0416666666666667+1.3533651474093098j) and (-1.0416666666666667-1.3533651474093098j)

Finding roots of quadratic equation video by Rajesh Sir:



Second Method:

import math

a = float (input("Enter the value of a : ")) b = float (input("Enter the value of b : ")) c = float (input("Enter the value of c : ")) dis = (b * b) - (4 * a * c) if(dis > 0): r1=(-b + math.sqrt(dis)) / (2 * a) r2=(-b - math.sqrt(dis)) / (2 * a) print("Two Distinct Real Roots are {0} and {1} ".format(r1, r2)) elif(dis == 0): r1 = r2 = -b / (2 * a) print("Two Equal are {0} and {1} ".format(r1, r2)) elif(dis < 0): r1 = r2 = -b / (2 * a) imaginary=math.sqrt(-dis) / (2 * a) print("Two Distinct Complex Roots Exists: r1 = %.2f+%.2f and r2 = %.2f-%.2f" %(r1, imaginary, r2, imaginary))

Output:

Enter the value of a : 1

Enter the value of b : 3

Enter the value of c : 2

Two Distinct Real Roots Exists: r1 = -1.00 and r2 = -2.00




146 views0 comments

Recent Posts

See All

Python program to calculate the factorial of a number

Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument. This program is most for O Level Practical Exam so read it care

NumPy program to find the most frequent value in the list.

This program is most for O Level Practical Exam so read it carefully. Program: import numpy as np x = np.array([1,2,3,4,5,7,2,1,1,1,8,9,1]) print(x) print("most frequent value in the list:") print(np.

bottom of page