How do you find prime numbers in a given range? [closed]

-1

I need some help with this program, on the for. Next, I need to print the prime numbers within this range A and B determined by the user, the problem is that the program only printed a number within that range. And also show whether or not there is a prime number within the range. Thanks in advance if anyone can help.

#include<stdlib.h>
#include<stdio.h>
main(){
int a, b, div=0;
printf("Digite o numero a:\n");
scanf("%d",&a);
if (a>=0 && a<=100){
    printf("Numero a esta entre 0 e 100\n",a);
}else{
    printf("Nao esta entre 0 e 100, digite novamente:\n", a);
    while(a>100){
        printf("Numero ivalido, digite novamente: \n");
        scanf("%d", &a);

    }
}
printf("Digite o numero b:\n");
scanf("%d",&b);
if (b>=0 && b<=100){
    printf("Numero b esta entre 0 e 100\n",b);

}else{
    printf("Nao estao entre 0 e 100, digite novamente:\n",b);
    while(b>100){
        printf("Numero invalido, digite novamente: \n");
        scanf("%d",&b);

    }

}
for(int i=a+1;i<b;i++){
    if(div%i==0){
        div++;
        printf("%d\n",i);

    }

}
if(div==2){
    printf("Sem numeros primos no intervalo");
}

}

    
asked by anonymous 06.11.2018 / 02:15

1 answer

1

I made a small functional example for you to analyze, remembering that the prime numbers can only be divided by itself and by one, so it is necessary more than one repetition structure in the code, any doubt contact me:)

int primo=0;
int a = 0;
int b = 1;
int tem_primo = 0;

printf("Numeros primos::.\n");

for(int i = a; i<=b; i++){

  //Inicia o verificador sempre como zero
  primo=0;

  for (int x = 1; x <=i; x++) {

    if(i % x == 0){
      primo++;
    }

  }
  if(primo==2){

    //Mostra na tela os numeros primos um a um
    printf("%d; ", i);
    tem_primo = 1;
  }
}

//Verifica se há primos no intervalo
if(tem_primo == 0){
  printf("\nNao possui primos no intervalo");
}
    
06.11.2018 / 03:45