Rule book:
1.)Must be Python -
2.) no imported module (ie cmath) - no can do on the imports... pure code..
3.) numbers are float - not integer.
Here's the prompt:
(Algebra: solve quadratic equations)
The two roots of a quadratic equation ax^2 + bx + c = 0
can be obtained using the following formula:
r1 = (-b + sqrt(b^2 - 4ac)) / (2a)
and
r2 = (-b - sqrt(b^2 - 4ac)) / (2a)
b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant.
If the discriminant is positive, display two roots.
If the discriminant is 0, display one root.
Otherwise, display “The equation has no real roots”.
Sample Run 1
Enter a: 1.0
Enter b: 3
Enter c: 1
The roots are -0.3819660112501051 and -2.618033988749895
Sample Run 2
Enter a: 1
Enter b: 2.0
Enter c: 1
The root is -1.0
Sample Run 3
Enter a: 1
Enter b: 2
Enter c: 3
The equation has no real roots
----------------------------------------------------------------------------------End of Prompt---------------------
For the sample run above... 1.0, 2 and 3
Here's my code (one of several variants): I'm close...my code produces roots of 2.45 and 3.54... but no cigar....maybe order of operations error?.. the solution is supposed to be -0.3819660112501051 and -2.618033988749895
Code below...
And... reminder... inputs are 1.0, 3 and 1, float values for inputs and no math modules - just code
#step one, have the user input values
a = float(input('Enter a value for a: '))
b = float(input('Enter a value for b: '))
c = float(input('Enter a value for c: '))
# calculate the discriminant
discriminant = (b*b - 4*a *c)
print('the discriminant is ',discriminant)
discRoot = discriminant ** (.05)
print('this is the square root of the discriminant ',discRoot)
if discriminant > 0:
print(f'The discriminant {discriminant} is\
greater than zero, so there are two roots.')
#calculate root1
root1 = (-b + (discRoot /2*a))
print(f'the first root is {root1}')
#calculate root2
root2 = (-b - (discRoot /2*a) )
print(f'the second root is {root2}')
elif discriminant == 0:
print(f'The discriminant {discriminant} is zero, so there is one
root')
else:
print(f'The discriminant is: {discriminant}, so the\
equation has no real roots')
Big thanks to the wizard who can help me out. ;)
Here is the corrected code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: I have also modified the output format to fit the expected output as sample runs
#code
#step one, have the user input values
a = float(input('Enter a value for a: '))
b = float(input('Enter a value for b: '))
c = float(input('Enter a value for c: '))
# calculate the discriminant
discriminant = (b*b - 4*a*c)
#to find square root, you should use **(.5) instead of
**(.05)
discRoot = discriminant ** (.5)
if discriminant > 0:
#two roots exist
#calculate root1
root1 = ((-b + discRoot) /2*a) #the brackets were
wrongly put before
#calculate root2
root2 = ((-b - discRoot) /2*a)
#printing the two roots
print(f'The roots are {root1} and
{root2}')
elif discriminant == 0:
#one single root exist
root = (-b / 2 * a)
#printing the single root
print(f'The root is
{root}')
else:
#no roots
print('The equation has no real
roots')
#output (multiple runs)
Enter a value for a: 1 Enter a value for b: 3 Enter a value for c: 1 The roots are -0.3819660112501051 and -2.618033988749895 Enter a value for a: 1 Enter a value for b: 2 Enter a value for c: 1 The root is -1.0
Enter a value for a: 1 Enter a value for b: 2 Enter a value for c: 3 The equation has no real roots
Rule book: 1.)Must be Python - 2.) no imported module (ie cmath) - no can do...
(Algebra: solve quadratic equations) The two roots of a quadratic equation, for example, , can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac) / (2a) and r1 = (-b - sqrt(b^2 - 4ac) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots....
In Python. The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac) / (2a) and r2 = (-b - sqrt(b^2 - 4ac) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no...
This is in python language
Algebra: solve quadratic equations) The two roots of a quadratic equation, for example, 0, can be obtained using the following fomula: b 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots. Write a program that prompts the user to enter values for a, b, and cand...
A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the above quadratic equation is computed using the following formula, root1 = (-b + sqrt(D))/ 2a root2 = (-b - sqrt(D))/2a Where D is the discriminant of the quadratic equation and is computed as, D = b^2 - 4ac Given the value of D, the roots of a quadratic equation can be categorized as follows, D > 0 : Two distinct real roots D =...
Question 4: Provide at least 4 test cases. Prompt the user to input 3 doubles, a, b and c. Which will represent the coefficients in the quadratic equation ax2 + bx + c = 0. Print out the solutions (if any) of the quadratic equation. If no root exists (this happens if a == 0, or b2 <4ac) print the message No real root. Sample input/ user entries shown in red Corresponding output Enter a, b and c which represent...
Use Python Programming.
Design a class named Quadratic Equation for a quadratic equation ax + bx+c 0. The class contains: • The data fields a, b, and c that represent three coefficients. . An initializer for the arguments for a, b, and c. • Three getter methods for a, b, and c. • A method named get Discriminant() that returns the discriminant, which is b- 4ac The methods named getRoot 1() and getRoot 2() for returning the two roots of...
reword m the program into a design document
diregard the m
Write a C++ program that solves a quadratic equation to find its roots. The roots of a quadratic equation ax2 + bx + c = 0 (where a is not zero) are given by the formula -b + b2 - 4ac 2a The value of the discriminant b2 - 4ac determines the nature of roots. If the value of the discriminant is zero, then the equation has a single...
write a function of quadratic choice. it must take in at least one parameter, and perform some task. the reslut will be returned to main, and printed. here is the sample but this sample still not complete because this sample still need to return root1 and root2 to the main. sample: def juneQuadratic(a,b,c): root1 = (-b + math.sqrt(b**2 - (4*a*c)) )/2*a root2 = (-b - math.sqrt(b**2 - (4*a*c)) )/2*a return (root1,root2) def main(): rootsTupleFromFunction = juneQuadratic(2,8,5) print(rootsTupleFunction) print("Root 1...
Using matlab and if/else statement please!
Write a function that determines the real roots of a quadratic equation ax2 + bx + c = 0. To calculate the roots of the equation, the function calculates the discriminant D, given by: D = b2-4ac If D> 0, the code should display "The equation has two roots" and print the values on a new line. If D 0, the code should display "The equation has one root.", and print the value on...
C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠ 0 are given by the following formula: In this formula, the term b² - 4ac is called the discriminant. If b² - 4ac = 0, then the equation has a single (repeated) root. If b² - 4ac > 0, the equation has two real roots. If b² - 4ac < 0, the equation has two complex roots. Instructions Write a program that prompts the...