AIM:write a java program to print all real solutions to
the qudratic eq ax2+b+c=0 Read a,b,c values and
use the formula (–b+sqrt(b2-4ac))/2a.
SOURCE CODE:
import java.io.*;
import java.lang.Math; class QuadraticEquation
{
public static void
main(String[] args) throws IOException
{
String s;
int a,b,c,d;
double r1,r2;
DataInputStream in=new
DataInputStream(System.in);
System.out.print("Enter a value");
s=in.readLine();
a=Integer.parseInt(s);
System.out.print("Enter b value");
s=in.readLine();
b=Integer.parseInt(s); System.out.print("Enter c
value?"); s=in.readLine(); c=Integer.parseInt(s); d=((b*b)-(4*a*c));
if(d<0)
{
System.err.println("No REAL solution...");
}
else
{
r1=((-b)+Math.sqrt(d))/(2*a);
r2=((-b)-Math.sqrt(d))/(2*a);
System.out.println("Root
1 = "+r1);
System.out.println("Root
2 = "+r2);
}
}
}
No comments:
Post a Comment