Write a program to accept the corresponding data to print the result of the following expression y = ax^2+bx+c
Answers
Let's start from the basics..
We cannot just ask python to solve a whole quadratic equation in its own! We will have to give it instructions.
To solve this, we will be using the quadratic formula by taking the inputs for a,b and c.
_____________________________________________
QUADRATIC FORMULA:
____________________________________________
Now, let's take inputs for the 3 numbers.
a,b,c = float(input('Enter a:')), float(input('Enter b:')),float(input('Enter c:'))
____________________________________________
Now, we will take one value for the operation under the root and give it a name so that we can utilize it after in the program.
import math #We will need math to work with square roots
sqrt_part = (math.sqrt((b**2) - (4*a*c)))
_____________________________________________
Now, we will have two programs, one for the + and the other for the -.
answer1 = ( -b + sqrt_part) / (2*a)
answer2 = ( -b - sqrt_part ) / ( 2 * a)
We know have both the answers.
___________________________________________-
CODE:
#If there are no complex numbers -- no 'i'/√-1
a,b,c = float(input('Enter a:')), float(input('Enter b:')),float(input('Enter c:'))
import math #We will need math to work with square roots
sqrt_part = (math.sqrt((b**2) - (4*a*c)))
answer1 = ( -b + sqrt_part) / (2*a)
answer2 = ( -b - sqrt_part ) / ( 2 * a)
print(answer1, answer2)
___________________________________________
CODE:
#If there are complex number, we use cmath instead of math (complex math)
a,b,c = float(input('Enter a:')), float(input('Enter b:')),float(input('Enter c:'))
import cmath
sqrt_part = (cmath.sqrt((b**2) - (4*a*c)))
answer1 = ( -b + sqrt_part) / (2*a)
answer2 = ( -b - sqrt_part ) / ( 2 * a)
print(answer1, answer2)
____________________________________________
Explanation:
class prog1
{
void main (int a ,int b,int c,int x)
{
double y;
y = a*(Math.pow(x,2))+b*x+c;
System.out.println("value of y="+y);
}
}