Java Programs
Write a program to find the roots of quadratic equation
import java.io.*;
import java.math.*;
import java.lang.*;
class quad
{
public static void main(String args[]) throws IOException
{
int a,b,c,dis;
double x,x1,x2,x3,x4,dis1,dis2;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Quadratic Equation ==> ax2+bx+c");
System.out.println("------------------");
System.out.println("Enter coefficient of x square");
a=Integer.parseInt(br.readLine());
System.out.println("Enter coefficient of x");
b=Integer.parseInt(br.readLine());
System.out.println("Enter constant c");
c=Integer.parseInt(br.readLine());
dis=(b*b)-(4*a*c);
if(dis>0)
{
x1=(-b + Math.sqrt(dis))/(2*a);
x2=(-b - Math.sqrt(dis))/(2*a);
System.out.println("Roots are distinct");
System.out.println("x=" + x1 + "," + x2);
}
else
if(dis==0)
{
x=(-b)/(2*a);
System.out.println("Only one root is distinct");
System.out.println("x=" + x);
}
else
if(dis<0)
{
x3=(-b)/(2*a);
dis1=((4*a*c)-(b*b))/(2*a);
System.out.println("No real roots are there only complex roots");
System.out.println("x=" + x3 + "+ " + dis1 + "i" + "," + x3 + "- " + dis1 +"i");
}
}
}
Write a program to find the roots of quadratic equation
import java.io.*;
import java.math.*;
import java.lang.*;
class quad
{
public static void main(String args[]) throws IOException
{
int a,b,c,dis;
double x,x1,x2,x3,x4,dis1,dis2;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Quadratic Equation ==> ax2+bx+c");
System.out.println("------------------");
System.out.println("Enter coefficient of x square");
a=Integer.parseInt(br.readLine());
System.out.println("Enter coefficient of x");
b=Integer.parseInt(br.readLine());
System.out.println("Enter constant c");
c=Integer.parseInt(br.readLine());
dis=(b*b)-(4*a*c);
if(dis>0)
{
x1=(-b + Math.sqrt(dis))/(2*a);
x2=(-b - Math.sqrt(dis))/(2*a);
System.out.println("Roots are distinct");
System.out.println("x=" + x1 + "," + x2);
}
else
if(dis==0)
{
x=(-b)/(2*a);
System.out.println("Only one root is distinct");
System.out.println("x=" + x);
}
else
if(dis<0)
{
x3=(-b)/(2*a);
dis1=((4*a*c)-(b*b))/(2*a);
System.out.println("No real roots are there only complex roots");
System.out.println("x=" + x3 + "+ " + dis1 + "i" + "," + x3 + "- " + dis1 +"i");
}
}
}