I have logical error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I have logical error

   public class Prime { public static void main (String[]args){ int num = 82; if (num ==0|| num ==1 ){ System.out.print("Number is not prime"); } else { for (int i = 2; i<num; i++) { if (num%i==0){ System.out.print("Number is not prime"); break; } else if (num%i!=0) { System.out.println("Number is prime"); break; } } } } } ye program sahi chal raha hai but Problem aa rahi hai ye 49 , 25, 9 ko prime number dikha raha hai Essa kyu Please check and resolve🙏

4th Sep 2020, 4:51 PM
Sid Harth Sharma
Sid Harth Sharma - avatar
12 Answers
+ 7
Sid Harth Sharma whom are you asking?
4th Sep 2020, 5:33 PM
Namit Jain
Namit Jain - avatar
+ 5
It will show that for all odd numbers! You have to check that all the values of i should not be a factor of the input! For that: public void IsPrime(int x) { if (x == 0 || x == 1) {return false;} for (int i = 0; i < x; i++) { if (x%i == 0) return false; } return true; }
4th Sep 2020, 5:29 PM
Namit Jain
Namit Jain - avatar
+ 5
Sid Harth Sharma In your code: In the first iteration itself you are breaking the loop! The iteration starts with 2 So, 2 cases are possible: Input%2 == 0 or Input%2!=0 So, if it is divisible so you are printing it's not a prime and breaking ✅ But If it is not divisible by 2 you have again used the break statement and claimed it is a prime! ❌ You have to check for 3 also and other numbers also! My code: Checks that all the values of i are not a factor of the input! And if it is of any of them then the number is not a prime but if any value of i is not a factor so it will show that the input is prime
4th Sep 2020, 5:40 PM
Namit Jain
Namit Jain - avatar
+ 5
// Try this public class Prime { public static void prime() { for(int num =2 ;num<=82; num++){ int count =0; for(int i =1 ; i<num;i++){ if(num%i==0){count++;} } if(count<2){ System.out.println(num); } } } public static void main(String[] args){ prime(); } }
6th Sep 2020, 4:03 AM
Dreamer
Dreamer - avatar
+ 3
else if (num%i!=0) { System.out.println("Number is prime"); break; } That is your problem. Your loop is misleading because when i = 2, either your if or your else case will be hit in first iteration of your loop. The loop is misleading when you don't iterate more than once. You need to finish the loop before determining that it is prime. Here is a corrected for-loop: boolean isPrime = true; // assume it is prime until proven otherwise. for (int i = 2; i<num; i++) { if (num%i==0){ System.out.print("Number is not prime"); isPrime = false; // we know it isn't now because num is divisible by i. break; } } // If no i was found between 2 and num - 1 such that num % i == 0, num must be prime. if (isPrime) { System.out.println("Number is prime"); break; }
4th Sep 2020, 5:16 PM
Josh Greig
Josh Greig - avatar
+ 3
Sid problem is condition me h ***else if (num%i!=0) *** me aarhi h. See one example let say mene number 9 liye ab wo jb apni else if (num%i!=0) wali condition me aaya to ho kya rha h behind the scene ki 9 ka mod 0 k equal nhi h to prime show kre , to jb apna i = 2 hua to usne 9 ko prime bta diya bcuz 9 is not complete divisible by 2 condition true, not equal to 0 h so usne Number is not prime print kr diya. Hope you got it :) I have made few changes on your code see below: public class Prime { public static void main(String[] args) { int num = 9; int flag = 0; if (num == 0 || num == 1) { System.out.print("Number is not prime"); } else { for (int i = 2; i < num; i++) { if (num % i == 0) { System.out.print("Number is not prime"); flag = 1; break; } } if (flag == 0) { System.out.println("Number is prime"); } } } }
4th Sep 2020, 5:27 PM
Puneet
+ 2
There are different rules for different numbers : Example For 3 sum of the digits must be divisible by 3 Apply the best way for different numbers A generic algorithm.will not be able to get the best results 🙏
6th Sep 2020, 4:55 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
My suggestion is remove that else if block completely and put another if block outside the for loop like this: if(i == sqrt(num)) { System.out.print("Number is prime) } Because if the number is prime and it gets divide by some value of i in the loop itself, the loop breaks out and the value of i will never reach num and hence this condition above will go false. But if i never completely divides num then this will turn true and num will be told prime. You could write in your counter questions too.
6th Sep 2020, 9:02 AM
विशेष
विशेष - avatar
+ 1
Say in English
6th Sep 2020, 11:04 AM
ramprasath s
0
Brother I can't understand Can u explain my error?
4th Sep 2020, 5:31 PM
Sid Harth Sharma
Sid Harth Sharma - avatar
0
Bro Namit can u explain it?
4th Sep 2020, 5:35 PM
Sid Harth Sharma
Sid Harth Sharma - avatar
0
There is a lot of unnecessary code in this. You checked if num is 0 or 1, and in that case num is not prime. So far so good. Getting in the loop. Firstly, you don't have to check the factors for num from 2 all the way to num. Just check it till sqrt(num). If there is any factor for num it will be found in the interval [2, sqrt(num)] and nowhere else. So don't unnecessarily go till num. The present algorithm is taking O(num) time but if you do what I suggested it will become O(num^1/2). Don't bother the big-O notation if you don't know about it. The other thing. If num%i==0 becomes a false condition, then num%i!=0 automatically becomes true so no need to check the condition again in the else if block, just write else. And think of what your current program will do. For every value of i which does not divides num, it will print "Number is prime" every single time, we don't want that. (Look at my next answer) 1/2
6th Sep 2020, 9:00 AM
विशेष
विशेष - avatar