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

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
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...
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
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  
Calculate the pH of a 0.45M solution of Benzonic Acid. (Use the quadratic equation to solve).
Calculate the pH of a 0.45M solution of Benzonic Acid. (Use the quadratic equation to solve).
1). Consider the quadratic equation x^2+ 100 x + 1 = 0 (i) Compute approximate roots...
1). Consider the quadratic equation x^2+ 100 x + 1 = 0 (i) Compute approximate roots by solving x^2 -100 x = 0 (ii) Use the quadratic formula to compute the roots of equation (iii) Repeat the computation of the roots but use 3 digit precision. (iv) Compute the relative absolute errors in the two 3 digit precision root approximations in (iii). (v) With x1 =1/2a (-b + sqrt b^2 - 4ac and x2 = 1/2a (-b + sqrt b^2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT