Thursday 26 March 2020

FINDING ROOTS OF A QUADRATIC EQUATION USING JAVA LANGUAGE

Q-30- The two roots of a quadratic equation 𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0  can be obtained using the following formula:

𝑟1 = −𝑏+√𝑏2−4𝑎𝑐 2𝑎         and           𝑟2 = −𝑏−√𝑏2−4𝑎𝑐 2𝑎
 b2 - 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 java 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” Note that you can use Math.pow(x, 0.5) to compute √𝑥

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double a ,b ,c;
double d ,r1 ,r2;
System.out.println("Enter The Value Of a ,b ,c :");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
d = b*b-4*a*c;
double D=Math.pow(d, 0.5);
r1=(-b+D)/2*a;
r2=(-b-D)/2*a;
if(d==0)
{
System.out.println("The Equation Has One Root "+r1);
}
else if(d>0)
{
System.out.println("The Equation Has Two Real Roots "+r1+" and "+r2);
}
else if(d<0)
{
System.out.println("The Equation Has No Real Roots.");
}


}


}

OUTPUT :
Enter The Value Of a ,b ,c :
1 3 1

The Equation Has Two Real Roots -0.3819660112501051 and -2.618033988749895

Enter The Value Of a ,b ,c :
1 2 1
The Equation Has One Root -1.0

Enter The Value Of a ,b ,c :
1 2 3
The Equation Has No Real Roots.


No comments:

Post a Comment