Python program to print the solutions of the given equation - Codicaly

 In this post, we are going to write a program to find the solutions of the inputted quadratic equation.



What are the Solutions of the Quadratic equation ?

A quadratic equation is an equation with degree 2, i.e, the maximum power of the variable is 2. The general form of the quadratic equation is as follows. 

Here 'a' is the coefficient of x^2, 'b' is the coefficient of x and c is the constant. The solutions to that equation can be easily determined using the coefficients and the constants. The formula for that is below.

Pre - requisites :

  • Python basics
Program :

a=int(input('Enter the coefficient of x^2 with sign'))
b=int(input('Enter the coefficient of x with sign'))
c=int(input('Enter the constant with sign'))
x1=(-b+(b**2-4*a*c)**(1/2))/(2*a)
x2=(-b-(b**2-4*a*c)**(1/2))/(2*a)
print("Solutions of the equation ",a,"x^2+(",b,")x +(",c,")",' are' ,x1,'and',x2)


Explanations :

1. To find the solution of the quadratic equation, we need to get the coefficients and constant of the equation.  Here, are inputting the coefficients and constants. Here we are specifying them as integers since we can do arithmetic operations only in integers and floats.



2. We need to process the data that we inputted to get the solution. To find the solution, we need to substitute the values of the coefficients and constants in the quadratic equation formula which is given above. Here, we are converting that equation into python form using python operators like ** for power, * for multiplication, + for addition, - for subtraction, and / for division. There will be two solutions as it is quadratic, we are assigning it x1 and x2.




3.After finishing the input and process, we need to get the output. Here we are printing the result.

Input/Output :

Let's try to find the solution of the equation, 

Here a=1,b=0 since there is no x and c=-4.


Sometimes, the solution may be imaginary. For that also, it prints the solution in imaginary form.



Thank You









Comments

Popular posts from this blog

Python Program to Print Fibonacci series - Codicaly

Python - Program to find the given number is a prime or composite number

Python Program To Replace The Elements of List With its Cube