import java.lang.*;
import java.util.*;
class Quadratic
{
public static void main(String args[])
{
int a,b,c,d;
double e,f;
System.out.println("Enter a,b,c values");
Scanner sc = new Scanner(System.in);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
System.out.println("Enterd values are a="+a+" b="+b+" c="+c);
d=(b*b)-(4*a*c);
if(d<0)
{
System.out.println("No real roots");
}else
{
e=((-b+Math.sqrt(d))/(2*a));
f=((-b-Math.sqrt(d))/(2*a));
System.out.println("Roots are "+e+" "+f);
}
}
}
The logic behind the program is very easy.
- at first we are reading three integer elements
- next we are calculating the "d" value that is the discrepant of the quadratic equation.
- if the value results lessthan d then the roots are not real
- else roots are real and we are printing both real and imaginary part of the equation
No comments:
Post a Comment
Note: only a member of this blog may post a comment.