Tag Archive | Root

WAP to find the Roots of a Quadratic Equation

import java.io.*;
class Quadratic
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the values of A, B and C of a Quadratic Equation : “);
int a=Integer.parseInt(br.readLine());
int b=Integer.parseInt(br.readLine());
int c=Integer.parseInt(br.readLine());
double s=(b*b)-(4*a*c);
if(s<0)
{
System.out.println(“The Roots are IMAGINARY”);
}
else
{
System.out.println(“The Roots are Real”);
}
double root1=((-b)+Math.sqrt(s))/(2*a);
double root2=((-b)-Math.sqrt(s))/(2*a);
System.out.println(“The Roots are “+root1+” and “+root2);
}
}

 

//Output

Enter the values of A, B and C of a Quadratic Equation :

1

-3

2

The Roots are Real
The Roots are 2.0 and 1.0