Prime no. between two nos

i cant find a logic to do this other than this

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;

while(t--)
{
    int pc=0;
    int a,b,j,i;
    cin>>a;
    cin>>b;
    if(a==1)
        a=a+1;
    for( i=a;i<=b;i++)
    {

        for( j=2;j<i;j++)
        {

            if(i%j==0)
                break;
        }
        if(j==i)
            pc++;
    }
    cout<<pc<<endl;

}

}

please help me in this

you need to learn sieve to find prime numbers in optimized way.
it is a part of Number theory.
After learning Sieve, you need to learn Segmented Sieve to find prime between to two numbers in more optimized way

Hey Aman, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required. And please mark your doubts as resolved in your course’s “Ask Doubt” section, when your doubt is resolved.