-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRootsQuad.java
45 lines (42 loc) · 1.29 KB
/
RootsQuad.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* A java program to find the roots of quadratic equation check whether the roots are real or imagenary.
*/
import java.util.Scanner;
class RootsQuad
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("a = ");
double a = sc.nextDouble();
System.out.print("b =");
double b = sc.nextDouble();
System.out.print("c = ");
double c = sc.nextDouble();
double d = ((b*b)-(4*a*c));
double sqrt = Math.sqrt(d);
double firstroot = 0,secoandroot = 0;
if(d > 0 )
{
firstroot = ((-b + sqrt)/(2*a));
secoandroot = ((-b -sqrt)/(2*a));
System.out.println("Roots are real and distinct : ");
System.out.println("First root : "+firstroot);
System.out.println("Secoand root : "+secoandroot);
}
else if(d == 0)
{
firstroot = ((-b + sqrt)/(2*a));
secoandroot = ((-b + sqrt)/(2*a));
System.out.println("Roots are equal :");
System.out.println("First root = Secoand root : "+firstroot+" = "+secoandroot);
}
else
{
firstroot = ((-b + sqrt)/(2*a));
secoandroot = ((-b + sqrt)/(2*a));
System.out.println("Roots are imagernary and distinct : ");
System.out.println("First root : "+firstroot);
System.out.println("Secoand root : "+secoandroot);
}
}
}