Question

In: Computer Science

Write a program to input the coefficients of a quadratic equation and solve roots for all...

Write a program to input the coefficients of a quadratic equation and solve roots for all cases (including complex roots). VBA.

x=(b ^2) - (4ac)

I have it for 2 complex and 2 real and repeated.

Is that all cases?

Solutions

Expert Solution

Module Root
Sub Main()
'variable declaration'
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim x1 As Double
Dim x2 As Double
Dim img As Double
Dim real As Double
Dim dis As Double
  
'variable initialization
Console.WriteLine("Enter the value of a, b, and c: ")
a = Convert.ToInt32(Console.ReadLine())
b = Convert.ToInt32(Console.ReadLine())
c = Convert.ToInt32(Console.ReadLine())

dis = b*b - 4 * a * c
  
'calculate solution
If dis>0 Then
x1 = (-b + Math.Sqrt(b*b - 4 * a * c)) / (2 * a)
x2 = (-b - Math.Sqrt(b*b - 4 * a * c)) / (2 * a)
  
'display the result
Console.WriteLine("There are two real roots and they are:")
Console.Write("x1 = ")
Console.WriteLine(x1)
Console.Write("x2 = ")
Console.Write(x2)
End If
  
If dis=0 Then
x1 = (-b + Math.Sqrt(b*b - 4 * a * c)) / (2 * a)
  
'display the result
Console.WriteLine("There is only one real root:")
Console.Write("x = ")
Console.WriteLine(x1)
End If
  
If dis<0 Then
img = Math.Sqrt(-dis) / (2 * a)
real = -b / (2 * a)
  
'display the result
Console.WriteLine("There are two complex roots and they are:")
  
If img >= 0 Then
Console.Write("x1 = ")
Console.Write(real)
Console.Write("-")
Console.Write(img)
Console.WriteLine("i")
  
Console.Write("x2 = ")
Console.Write(real)
Console.Write("+")
Console.Write(img)
Console.Write("i")
End If
  
If img < 0 Then
Console.Write("x1 = ")
Console.Write(real)
Console.Write(img)
Console.WriteLine("i")
  
Console.Write("x2 = ")
Console.Write(real)
Console.Write("+")
Console.Write(-img)
Console.Write("i")
End If
End If
End Sub
End Module

The screenshot of the above source code is given below:

OUTPUT:


Related Solutions

Find Roots of a Quadratic Equation Step 1: Analyze the Problem Accept three coefficients a, b,...
Find Roots of a Quadratic Equation Step 1: Analyze the Problem Accept three coefficients a, b, and c (all of data type double) of a quadratic equation: ax2+bx+c=0 Output real roots (double) of the equation Step 2: Develop a Solution Check for degenerate case (user enters 0 for both a and b), no solution Check if the equation is linear (user enters 0 for a), x = -c/b Calculate the discriminant = b2-4ac (obviously, the data type will be double)...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program,...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program, quadroots.m, for finding the roots of the second- order polynomial ax2 + bx + c. Use the quadratic equation. The inputs are the coefficients a,b, and c and the outputs are z1 and z2. The program should produce (exactly) the following output in the Command window when run with (a, b, c) = (1, 2, −3):
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 +...
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 + bx + c = 0 where coefficient a is not equal to 0. The equation has two real roots if its discriminator d = b2 – 4ac is greater or equal to zero. The program gets the three coefficients a, b, and c, computes and displays the two real roots (if any). It first gets and tests a value for the coefficient a. if...
Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
Draw a flowchart that will calculate the roots of a quadratic equation. (java) Can someone draw...
Draw a flowchart that will calculate the roots of a quadratic equation. (java) Can someone draw it for me? and at same time explain,
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula...
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula ( you must do it both ways). Show all steps for each method and put your answer in simplest radical form possible. 2) Which part of the Quadratic Formula can help you to find the Nature of the roots for the Quadratic Equation. Explain how you can find the nature of the roots and provide one Example for each possible case with solution.
use Java The two roots of a quadratic equation ax^2 + bx + c = 0...
use Java 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...
Quadratic Equation.
If a² + b² + c² = 1, then ab + bc + ac lies in which interval
Quadratic Equation
If one root is square of the other root of the equation x²+px+q=0, then find the relation between p and q.
Quadratic equation.
If the roots of the quadratic equation x² + px + q = 0 are tan 300 and tan 150, respectively then the value of 2+q-p is(a) 2(b) 3(c) 0(d) 1  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT