Can someone tell me where i am getting wrong

import java.util.*;
public class PrimeVisits {
public static void main(String args[]) {
Scanner scan=new Scanner(System.in);
// testcases
int t = scan.nextInt();
ArrayList list = new ArrayList<>();
while(t>0){
// input a and b
//boolean flag = true;
int count=0;
int a = scan.nextInt();
int b = scan.nextInt();
for(int i=a;i<=b;i++){
//for ith element
boolean flag = true;
for(int j=2;j<=i-1;j++){
if(i%j==0){
flag=false;
}
}
if(flag==false){
count++;
}
}
list.add(count);
t–;
}
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}

}

}

Hi Vaibhav

You basically have to count the prime numbers between a and b in this question. Your code is doing otherwise.
For a = 1 and b = 10, there are 4 prime numbers in between them, i.e., 2, 3, 5, and 7. Hence, the output is 4.

Also, please share your code by pasting it in ide.codingblocks.com first, saving it and then sending the link here. It’ll help us to debug the code better.

Third testcase time limit error import java.util.*; public class Main { public static void main(String args[]) { Scanner scan=new Scanner(System.in); // testcases int t = scan.nextInt(); ArrayList list = new ArrayList<>(); while(t>0){ // input a and b //boolean flag = true; int count=0; int a = scan.nextInt(); int b = scan.nextInt(); int i=a; if(a<=1){ i=2; } for(;i<=b;i++){ //for ith element boolean flag = true; for(int j=2;j<=(int)java.lang.Math.pow(i,0.5);j++){ if(i%j==0){ flag=false; } } if(flag==true){ count++; } } list.add(count); t–; } for(int i=0;i<list.size();i++){ System.out.println(list.get(i)); } } }

Hi Vaibhav,
This question has to be done through sieve method.

Hi @Vaibhav-Garg-1998823966894298
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.