Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
Python program to find roots of a quadratic equation
Roots of a quadratic equation
In this python program, we will learn how to find the roots of a quadratic equation[ax2 + bx + c]. When we try to solve the quadratic equation we find the root of the equation. Mainly roots of the quadratic equation are represented by parabola in 3 different patterns like
- No Real Roots
- One Real Root
- Two Real Roots
When we solve the equation we get 3 conditions mentioned above using this formula:-
X = [-b (+or-)[Squareroot(pow(b,2)-4ac)]]/2a
- Step 1:- Start.
- Step 2:- Take user inputs for a,b and c.
- Step 3:- Check if the value of a is not zero.
- Step 4:- Calculate Functions value ie, b*b – a*a*c.
- Step 5:- Find the square root of the function.
- Step 6:-if the value is greater than zero print Two real roots and value of roots.
- Step 7:- If the value is equal to zero Print one real root and print the value.
- Step 8:- If both the condition are false print no real roots and print values.
Python program to find the square root of a quadratic equation
#import math library import math #take user inputs a = int(input('Enter value of a :')) b = int(input('Enter value of b :')) c = int(input('Enter value of c :')) #check for value of a if a == 0: print("a cannot be zero") #if a is greater than 0 else: #calculate value of Function val = b**2 - 4 * a * c root = math.sqrt(abs(val)) #Check for roots and print according to their nature if val > 0: print("Two Real Roots") print((-b + root)/(2 * a)) print((-b - root)/(2 * a)) elif val == 0: print("One Real Root") print(-b / (2*a)) else: print("No Real Root") print(- b / (2*a) , " + i", root) print(- b / (2*a) , " - i", root)
Enter value of a :1
Enter value of b :-7
Enter value of c :12
Two Real Roots