06 September, 2012

All real solutions to the quadratic equation

Java program that prints all real solutions to the quadratic equation ax2+bx+c=0.


import java.lang.*;
import java.util.*;
class Quadratic
{
public static void main(String args[])
{
int a,b,c,d;
double e,f;

Scanner s = new Scanner(System.in);

System.out.println("Enter value a = ");
a=s.nextInt();
System.out.println("Enter value b = ");
b=s.nextInt();
System.out.println("Enter value c = ");
c=s.nextInt();
System.out.println("Your equation : "+a+"x2 + "+b+"x + "+c+" = 0");

d=(b*b)-(4*a*c);
if(d<0)
{
    System.out.println("No real roots");
}
else
{
    e=((-b+Math.sqrt(d))/(2*a));
    f=((-b-Math.sqrt(d))/(2*a));
    System.out.println("Roots are :"+e+" , "+f);
}

}
}

1 comment:

Thank You !