Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //prototipo
- int* criaPrimos(int qtde);
- //funçao main
- int main()
- {
- int qtde,i;
- printf("Quantos numeros primos?\n");
- scanf("%d",&qtde);
- int *vet2;//vetor auxiliar
- vet2=criaPrimos(qtde);//vetor auxiliar recebe o vet
- for (i=0; i<qtde; i++,vet2++)
- printf("Mostre vet[%d] = %d\n",i,*vet2);//exibe o vetor
- free(vet2);//limpar o vetor
- return 0;
- }
- //funçao cria prims
- int* criaPrimos(int qtde)
- {
- int j,k=0,aux,i;
- int *vet = (int*)malloc(sizeof(int)*qtde);//aloca espaç na memoria
- for (j=2; k<qtde; j++){
- for (i=1,aux=0; i<j; i++)//descolbre se é primo
- if (j%i==0)
- aux++;
- if (aux==1){//se aux=1 preenche vetor
- vet[k]=j;
- k++;}}
- return vet;
- }
Advertisement
Add Comment
Please, Sign In to add comment