0

What is logical error?? Please explain to me??

package Play; public class Test { public static void main (String[]args) { int number = 99; if (number==0 || number==1) { System.out.println("Number is Prime"); } else{ for(int I = 2; i < number; i++) { if ( number % i ==0) { System.out.println (" Number is not Prime"); break; } else { System.out.println("Number is Prime"); break; } } } Output Number is Prime

19th Mar 2021, 9:33 AM
Sid Harth Sharma
Sid Harth Sharma - avatar
4 Respostas
+ 1
You have a capital I in the for loop. Make it lowercase because any other appearance of i is lowercase and Java will think they're different things. On the logic side, you can't use else statement according to your code as it makes your number checked only once. The for loop will be useless. The prime numbers work only if the loop ends without if(number % i == 0) being true..
19th Mar 2021, 10:03 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
First, 0 cannot be prime because it can be divided by any other number except 0
19th Mar 2021, 10:07 AM
Eddy M
+ 1
And you can only decide if it's prime after checking with all the numbers you need to check
19th Mar 2021, 10:38 AM
Eddy M