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
π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.");
}
}
}
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