0
Please tell me my fault why this code is not getting compiled
import java.util.Scanner; public class Program { public static void main(String[] args) { double x ; Scanner sc = new Scanner(System.in); double num ; System.out.println(" enter a no."); num = sc.nextDouble(); if(num < 0); { System.out.println(" this is not a postive no "); System.out.println(" existing Program "); System.exit(0); } else { result = Math.sqrt(num); } System.out.println(" this is sqrt of " + result + " the given no. " + x ); } } https://code.sololearn.com/c9o1AT4xm6aR/?ref=app
14 Answers
+ 3
Vikash Kumar Tiwari
Semicolon used to break statement so if you use semicolon after if block then condition will break there and else part will give error because there is no if part for else.
+ 2
Vikash Kumar Tiwari
Try this
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double result = 0.0;
double num;
System.out.println(" enter a no.");
num = sc.nextDouble();
if(num < 0) {
System.out.println(" this is not a postive no ");
System.out.println(" existing Program ");
System.exit(0);
} else {
result = Math.sqrt(num);
}
System.out.println(" this is sqrt of " + result + " the given no. " + num);
}
}
0
Plz answer fastly
0
You have to initialize x and result double result = 0; double x = 0;
In if statement you have to remove the semi-colon.
0
No plz could u just correct the code
0
And send me the answer
0
Yaa that I understood
0
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
double x = 0 ;
Scanner sc = new Scanner(System.in);
double num = 0 ;
System.out.println(" enter a no.");
num = sc.nextDouble();
if(num < 0)
{
System.out.println(" this is not a postive no ");
System.out.println(" existing Program ");
System.exit(0);
}
else
{
result = Math.sqrt(num);
}
System.out.println(" this is sqrt of " + result + " the given no. " + x );
}
}
See its not compiling
0
No the are faulty square braces in else statement.See my code again
0
I thank u
0
For ur precious help
0
I got my mistake
0
fuerhermore, the else statement is unnecessary. you can remove it