Time limit exceeds

2 out of 3 test cases are working but the time limit of first is exceeding. I can’t figure out what to do.

#include <bits/stdc++.h>
using namespace std;

int main()
{

    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
    int c=0;
    int flag=0;
    int a;
    cin>>a;
    int b;
    cin>>b;
    for (int i = a; i <= b; i++)  
    { 
    
    if (i == 1 || i == 0) 
        continue; 

    flag = 1; 

    for (int j = 2; j <=sqrt(i); ++j)  
    { 
        if (i % j == 0)  
        { 
            flag = 0; 
            break; 
        } 
    } 

    
    if (flag == 1) 
        ++c; 
    }
    cout<<c<<endl;
    }

}

@rakshitgangwar The naive approach for generating the prime numbers will fail for large test cases in this problem. You have to try it using Sieve of Eratosthenes whose lecture videos are available in the Number Theory section of your course. Try this problem after referring that.

I am using Sieve of Eratosthenes method but it is showing timelimkt

Please share your implementation using Sieve of Eratosthenes method.
Save your code on ide.codingblocks.com and share its link.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

same issue
here is my code

#include
using namespace std;
#include
int main()
{
int n;
cin >> n;
if (n != 2 && n <= 1000000000)
{
float a;
a = pow(n, 0.5);
if (n % 2 != 0)
{
for (int i = 3; i <= a; i + 2)
{
if (n % i == 0)
{ cout << “Not Prime” << endl;
return 0;
}
}
cout << “Prime” << endl;
}
else
cout << “Not Prime” << endl;
}

return 0;

}

https://online.codingblocks.com/app/player/174427/content/167568/4775/code-challenge#