# Field Manual for Hackers

## Java Write a program to find all the types of roots of a given quadratic equation.

1. import java.util.Scanner;

class Test{

public static void main(String ar[])
{

Scanner sc=new Scanner(System.in);
double a,b,c,root1,root2,realp,imagp,disc;
System.out.println("Enter Value of A, B and C");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();

if (a==0 || b==0 || c==0){
System.out.println("Error: Roots cannot be determined \n");
}
else{
disc=b*b-4.0*a*c;
if(disc<0)
{
System.out.println("Imaginary Roots");
realp=-b/(2.0*a);
imagp=Math.sqrt(Math.abs(disc))/(2.0*a);
System.out.println("Root1 = "+realp+" +i "+imagp);
System.out.println("Root2 = "+realp+" -i "+imagp);
}
else if(disc==0)
{
System.out.println("Roots are real and equal");
root1=-b/(2.0*a);
root2=root1;
System.out.println("Root1 = "+root1);
System.out.println("Root2 = "+root2);
}
else
{
System.out.println("Roots are real and distinct");
root1=(-b+(Math.sqrt(disc))/(2.0*a));
root2=(-b-(Math.sqrt(disc))/(2.0*a));
System.out.println("Root1 = "+root1);
System.out.println("Root2 = "+root2);
}
}

}
}

2. import java.util.Scanner;
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
class Test
{

public static void main(String ar[])
{

Double a,b,c,det,root1,root2,sqdet,det2;
System.out.println("For equation: a(x^2)+b(x)+c\nEnter the value of a,b,c respectively: ");
Scanner obj=new Scanner(System.in);
a=obj.nextDouble();
b=obj.nextDouble();
c=obj.nextDouble();
det=(b*b)-4*a*c;

if(det==0)
{ System.out.println("Equal roots exist");
root1=-b/2*a;
root2=-b/2*a;
System.out.println("Roots are"+root1+root2);
}
else if(det>0){
System.out.println("Real and distinct roots exist");
sqdet=Math.sqrt(det);
root1=(-b+sqdet)/2*a;
root2=(-b-sqdet)/2*a;
System.out.println("Roots are"+root1+root2);
}
else
{
System.out.println("Imaginary roots exist");
root1=-b/2*a;
det2=Math.abs(det);
root2=Math.sqrt(det2);
System.out.print("Roots are: "+root1);
System.out.print("+i"+root2);
System.out.print(" and "+root1);
System.out.print("+i"+root2);
}

}
}

3. import java.util.*;
{
public static void main(String args[])
{
float b,a,c,qua;
System.out.println("Enter number");
Scanner sc=new Scanner(System.in);
b=sc.nextFloat();
a=sc.nextFloat();
c=sc.nextFloat();
qua=b*b-4*a*c;
System.out.println(qua);
}
}

4. import java.util.Scanner;
public static void main(String ar[]){
Scanner obj=new Scanner(System.in);
double root1,root2;
int a,b,c,det;
System.out.println("enter the values of a,b,c in ax^2+bx+c=0 respectively");
a=obj.nextInt();
b=obj.nextInt();
c=obj.nextInt();
det=(b*b)-4*(a*c);
while(det>=0){
root1=(-b+Math.sqrt(det))/2*a;
root2=(-b-Math.sqrt(det))/2*a;
System.out.println("roots of given quad eqn are"+root1+" and "+root2);
break;
}
if(det<0)
System.out.println("roots dont exist");
}
}

5. import java.util.Scanner;
{
public static void main(String args[])
{
float b,a,c,qua;
System.out.println("Enter number \n");
Scanner s=new Scanner(System.in);
System.out.println("Enter value of a: \n");
a=s.nextFloat();
System.out.println("Enter the value of b: \n");
b=s.nextFloat();
System.out.println("Enter the value of c: \n");
c=s.nextFloat();
qua=b*b-4*a*c;
System.out.println(qua);
}
}