Please tell me my fault why this code is not getting compiled | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

5th Feb 2021, 7:16 PM
Vikash Kumar Tiwari
Vikash Kumar Tiwari - avatar
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.
5th Feb 2021, 7:32 PM
A͢J
A͢J - avatar
+ 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); } }
5th Feb 2021, 7:35 PM
A͢J
A͢J - avatar
0
Plz answer fastly
5th Feb 2021, 7:17 PM
Vikash Kumar Tiwari
Vikash Kumar Tiwari - avatar
0
You have to initialize x and result double result = 0; double x = 0; In if statement you have to remove the semi-colon.
5th Feb 2021, 7:29 PM
HBhZ_C
HBhZ_C - avatar
0
No plz could u just correct the code
5th Feb 2021, 7:31 PM
Vikash Kumar Tiwari
Vikash Kumar Tiwari - avatar
0
And send me the answer
5th Feb 2021, 7:31 PM
Vikash Kumar Tiwari
Vikash Kumar Tiwari - avatar
0
Yaa that I understood
5th Feb 2021, 7:33 PM
Vikash Kumar Tiwari
Vikash Kumar Tiwari - avatar
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
5th Feb 2021, 7:34 PM
Vikash Kumar Tiwari
Vikash Kumar Tiwari - avatar
0
No the are faulty square braces in else statement.See my code again
5th Feb 2021, 7:37 PM
HBhZ_C
HBhZ_C - avatar
0
I thank u
5th Feb 2021, 7:38 PM
Vikash Kumar Tiwari
Vikash Kumar Tiwari - avatar
0
For ur precious help
5th Feb 2021, 7:38 PM
Vikash Kumar Tiwari
Vikash Kumar Tiwari - avatar
0
I got my mistake
5th Feb 2021, 7:39 PM
Vikash Kumar Tiwari
Vikash Kumar Tiwari - avatar
0
fuerhermore, the else statement is unnecessary. you can remove it
7th Feb 2021, 7:58 AM
John Doe