Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:5)
I am not able to understand the error
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d= b*b-(4*a*c);
if(a==9){
System.out.println("linear equation");
if(d>0){
double r1 = -b-Math.sqrt(d)/(2*a);
double r2 = -b+Math.sqrt(d)/(2*a);
System.out.println("Real but Distinct");
if(r1>r2)
System.out.println(r2+" "+r1);
else
System.out.println(r1+" "+r2);
}
else if(d==0){
double r1=-b/(2*a);
double r2=r1;
System.out.println("Real and Equal");
}
else
{ System.out.println("Imaginary");
}
}
}
}
My code is not passing the test cases why?
Note : Print only the integer part of the roots.
Just tell me , why are you using this condition if(a==9){
System.out.println(“linear equation”);
r1 and r2 and getting printed without double data type
and its not a==9 but a==0 because then it will not be a quadratic equation
Try for this input
1 -11 28
you are doing wrong
with these input also I am unable to pass the test cases
Debug your code first.
If you are not able to write a correct code. I will send you
import java.util.;
public class Main {
public static void main(String args[]) {Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d= bb-(4ac);
if(a==9){
System.out.println(“linear equation”);
if(d>0){
double r1 = -b-Math.sqrt(d)/(2*a);
double r2 = -b+Math.sqrt(d)/(2*a);
int r3 = (int)r1;
int r4 = (int)r2;
System.out.println("Real but Distinct");
if(r3>r4)
System.out.println(r4+" "+r3);
else
System.out.println(r3+" "+r4);
}
else if(d==0){
double r5=-b/(2*a);
double r6=r5;
int r7=(int)r5;
int r8=(int)r6;
System.out.println("Real and Equal");
System.out.println(r7+" "+r8);
}
else
{ System.out.println("Imaginary");
}
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d= b*b-(4*a*c);
if(d>0){
double r1 = -b-Math.sqrt(d)/(2*a);
double r2 = -b+Math.sqrt(d)/(2*a);
int r3 = (int)r1;
int r4 = (int)r2;
System.out.println("Real but Distinct");
if(r3>r4)
System.out.println(r4+" "+r3);
else
System.out.println(r3+" "+r4);
}
else if(d==0){
double r5=-b/(2*a);
double r6=r5;
int r7=(int)r5;
int r8=(int)r6;
System.out.println("Real and Equal");
System.out.println(r7+" "+r8);
}
else
{ System.out.println("Imaginary");
}
}
}
still one test case is not resolved
correct code :
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = ((b * b - (4 * a * c)));
if (d > 0) {
int r1 = (int) ((-b -Math.sqrt(d)) / (2 * a));
int r2 = (int) ((-b +Math.sqrt(d) )/ (2 * a));
System.out.println("Real and Distinct");
if (r1 < r2)
System.out.println(r1 + " " + r2);
else
System.out.println(r2 + " " + r1);
} else if (d == 0) {
int r5 = -b / (2 * a);
int r6 = r5;
System.out.println("Real and Equal");
System.out.println(r5 + " " + r6);
}
else {
System.out.println("Imaginary");
}
}
}