This code is to solve a quadric equation in Java. It output puts Nan
.
What is the wrong and how can I resolve this?
I tried a=1, b=2, c=10
.
import java.util.*;
class equation {
public static void main(String args[]) {
int a, b, c;
String input;
Scanner key = new Scanner(System.in);
System.out.println("Enter a value for a: ");
a = key.nextInt();
System.out.println("Enter a value for b: ");
b = key.nextInt();
System.out.println("Enter a value for c: ");
c = key.nextInt();
//finding roots
double temp = Math.sqrt(b * b - 4 * a * c);
double root1 = (-b + temp) / (2 * a);
double root2 = (b + temp) / (2 * a);
System.out.println("Answers are " + root1 + " or " + root2 + " .");
}
}