Java query | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java query

Why is it showing 9 in result? https://code.sololearn.com/c3q8zB7CVM9N/?ref=app

26th Mar 2019, 5:29 AM
Abhay Jindal
Abhay Jindal - avatar
1 Answer
+ 1
Your method dont test really if a number is prime or not because it print to first test case the number checking if its divisible by 2 and if not print the number (in practice it print odd numbers)... Futhermore in the inner loop you use the i var than make more confusion and problems... Try this code : for(int i =a;i<=b;i++){ boolean isPrime= true; for (int j=2;j<=i/2;j++){ if (i%j==0){ isPrime= false; break; } } if(isPrime) System.out.print(i +" "); }
26th Mar 2019, 5:46 AM
KrOW
KrOW - avatar