The roots of the quadratic equation ax^2+bx+c=0, where a,b,c are real numbers an
ID: 3626535 • Letter: T
Question
The roots of the quadratic equation ax^2+bx+c=0, where a,b,c are real numbers and a does not =0 are given by the formula [ -b + sqrt(b2 - 4ac) ] / (2a). Write a program that prompts the user to input the value of 1(coefficient of x^2) b (coefficient of x) and c(constant term) and outputs the types of roots of the equation. If b^2-4ac>0 the program should output the roots of the quadratic equation (use method sqrt from class math to calculate the square root.math,sqrt(x) isthe square root of x, where x>=0.The roots of the quadratic equation ax^2+bx+c=0, where a,b,c are real numbers and a does not =0 are given by the formula [ -b + sqrt(b2 - 4ac) ] / (2a). Write a program that prompts the user to input the value of 1(coefficient of x^2) b (coefficient of x) and c(constant term) and outputs the types of roots of the equation. If b^2-4ac>0 the program should output the roots of the quadratic equation (use method sqrt from class math to calculate the square root.math,sqrt(x) isthe square root of x, where x>=0.
Explanation / Answer
import java.util.*;
class square
{
public static void main(String[] a2)
{
Scanner in = new Scanner(System.in);
System.out.println("enter first value a :");
int a=in.nextInt();
System.out.println("enter second value b :");
int b=in.nextInt();
System.out.println("enter third value c :");
int c=in.nextInt();
System.out.println("roots are given by : ");
if(b*b-4*a*c>=0)
{
double x = (-b + Math.sqrt(b*b-4*a*c) )/2;
double y = (-b - Math.sqrt(b*b-4*a*c) )/2;
System.out.println("x is " + x + " y is " + y);
}
else
{
System.out.println("roots are complex :");
double kp = -1*(b*b-4*a*c);
System.out.println("x is " + (-b/2)+ "+" + "j" + (kp/2));
System.out.println("y is " + (-b/2)+ "-" + "j" + (kp/2));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.