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
4 Answers
+ 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..
+ 1
First, 0 cannot be prime because it can be divided by any other number except 0
+ 1
And you can only decide if it's prime after checking with all the numbers you need to check