help me find a better algorithm for primes

i need to make a program that finds all primes between some numbers

the biggest number is 1000000000.

the code that i have works fine, the only problem is that what i thought that would solve this, is too slow

Code:

`#include <stdio.h>`

int main(){

FILE *fin=fopen("in.txt","r");

int t,n1[10],n2[10],i,j,ex,k;

fscanf(fin,"%d\n",&t);

for (i=1;i<=t;i++){

fscanf(fin,"%d %d\n",&n1[i],&n2[i]);

}

fclose(fin);

FILE *fout=fopen("out.txt","w");

for (k=1;k<=t;k++){

for (i=n1[k];i<=n2[k];i++){

ex=0;

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

if (i%j==0){

ex=ex+1;

}

}

if (ex==2){

fprintf(fout,"%d\n",i);

}

}

fprintf(fout,"\n");

}

fclose(fout);

return 0;

}

i want to make it faster, like, 5 secs to find all primes until number 1000000000.

cant think anything right now, well imean a better algorithm, so would appreciate any help provided.

thanks