a)
Write a
Java program that prints all real solutions to the quadratic equation ax2
+ bx +c = 0. Read in a, b, c and use the quadratic formula. If the discriminant
b2- 4ac is negative, display a message stating that there are no
real solutions.
Program:
import
java.io.*;
class
SolvingEquation
{
int desc;
double r1,r2;
void solution(int a,int b,int c)
{
desc=(b*b)-(4*a*c);
if(desc>0)
{
r1=(-b+Math.sqrt(desc))/(2*a);
r2=(-b-Math.sqrt(desc))/(2*a);
System.out.print("\nThe
Roots Are: "+r1+"\t"+r2);
}
else if(desc==0) System.out.print("\nThe Roots
Are Equal:"+(-b/(2*a)));
else System.out.print("\nThere
Are No Real Solutions");
}
public static void main(String args[])
throws IOException
{
int a,b,c;
SolvingEquation obj=new
SolvingEquation();
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter value
of a:");
a=Integer.parseInt(br.readLine());
System.out.print("\nEnter value
of b:");
b=Integer.parseInt(br.readLine());
System.out.print("\nEnter value of
c:");
c=Integer.parseInt(br.readLine());
obj.solution(a,b,c);
}
}
Output:
E:\Lab>javac
SolvingEquation.java
E:\Lab>javac
SolvingEquation.java
Enter value of
a:2
Enter value of
b:3
Enter value of
c:1
The Roots Are:
-0.5 -1.0