Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void verificacaoDePrimos(int* vetor, int* numero)
- {
- int i,j,k,n,l;
- int aux;
- n=*numero-2;
- for(i=0;i<n;i++)
- {
- l=0;
- for(j=1;j<n;j++)
- {
- if((vetor[i+j])%(vetor[i])==0)
- {
- for(k=i+j;vetor[k]<vetor[k+1];k++)
- {
- aux=vetor[k];
- vetor[k]=vetor[k+1];
- vetor[k+1]=aux;
- }
- n--;
- l++;
- }
- }
- if(l!=0)
- {
- aux=*numero;
- vetor=(int*)realloc(vetor,(n+1)*sizeof(int));
- if(vetor==NULL)
- {
- printf("Fudeu");
- }
- }
- }
- for(i=0;i<=n;i++)
- {
- printf("%d ",vetor[i]);
- }
- }
- void formaVetor(int* numero)
- {
- int i,valor;
- int* vetor;
- valor=*numero;
- vetor=(int*)malloc((valor-1)*sizeof(int));
- if(vetor==NULL)
- {
- printf("Nao tem espaco disponivel na memoria!");
- exit(1);
- }
- else
- {
- for(i=2;i<=valor;i++)
- {
- vetor[i-2]=i;
- }
- verificacaoDePrimos(vetor,numero);
- }
- free(vetor);
- }
- int main()
- {
- int numero;
- do
- {
- printf("Digite o numero e veja todos os primos ate o numero digitado: ");
- scanf("%d",&numero);
- }while(numero<=1);
- formaVetor(&numero);
- return 0;
- }
Add Comment
Please, Sign In to add comment