Program to find roots of a Quadratic Equation ax^2 +bx+c=0.

import java.io.*;
public class test
{

public static void main(String[] args)throws IOException
{
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
int a,b,c;
double root, root1, root2;
String num;
System.out.println("Enter the values of constants of the equation, a,b,c::");
num=obj.readLine();
a=Integer.parseInt(num);
num=obj.readLine();
b=Integer.parseInt(num);
num=obj.readLine();
c=Integer.parseInt(num);
root=(b*b - 4*a*c);
if(root >0) System.out.println("Equation has two different real roots.");
else System.out.println("Equation does not have two different roots.");
root1=(-b+Math.sqrt(root))/(2*a);
root2=(-b-Math.sqrt(root))/(2*a);
System.out.println("Roots are "+root1+"and"+root2+".");
}

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s