Sunday, January 20, 2008

Quadratic Equation

Wap to input the values of three coefficients of a quadratic equation
AX(Square)+BX+C=0
Find the roots of the Equation and display them , stating wether they are real,imaginary or equal
Also print the value of X

import java.io.*;
class Quadratic
{
public int a,b,c;
public void Quadratic()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter the value A");
int a = Integer.parseInt(br.readLine());
System.out.println(" Enter the value B ");
int b = Integer.parseInt(br.readLine());
System.out.println(" Enter the value C ");
int c = Integer.parseInt(br.readLine());
}
public void Values()
{
double x1,x2;
int z = (b*b)-(4*a*c);
if(z>0)
{
System.out.println(" Real Roots ");
x1=(-b+Math.sqrt(b*b-4*a*c))/(2*a);
System.out.println( " Root 1 " + x1);
x2=(-b-Math.sqrt(b*b-4*a*c))/(2*a);
System.out.println( " Root 2 " + x2);
}
else if(z==0)
{
System.out.println(" Equal Roots ");
x1= (b)/(2*a);
System.out.println(" Root = " +x1);
}
else if(z<0)
{
System.out.println(" Imaginary Roots ");
}
}
public static void main(String args[])throws IOException
{
Quadratic Q = new Quadratic();
Q.Quadratic();
Q.Values();
}
}

1 comment:

golu said...

there is error in the program
(-)is missing in variable b when z=0.So please correct it soon.
By varun & SACHIN