FranciscoSoccol

Lista 08 - Exercicio 13

Aug 10th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //prototipo
  4. int* criaPrimos(int qtde);
  5. //funçao main
  6. int main()
  7. {
  8.     int qtde,i;
  9.     printf("Quantos numeros primos?\n");
  10.     scanf("%d",&qtde);
  11.     int *vet2;//vetor auxiliar
  12.     vet2=criaPrimos(qtde);//vetor auxiliar recebe o vet
  13.     for (i=0; i<qtde; i++,vet2++)
  14.         printf("Mostre vet[%d] = %d\n",i,*vet2);//exibe o vetor
  15.     free(vet2);//limpar o vetor
  16.     return 0;
  17. }
  18. //funçao cria prims
  19. int* criaPrimos(int qtde)
  20. {
  21.     int j,k=0,aux,i;
  22.     int *vet = (int*)malloc(sizeof(int)*qtde);//aloca espaç na memoria
  23.     for (j=2; k<qtde; j++){
  24.         for (i=1,aux=0; i<j; i++)//descolbre se é primo
  25.             if (j%i==0)
  26.                aux++;
  27.         if (aux==1){//se aux=1 preenche  vetor
  28.             vet[k]=j;
  29.             k++;}}
  30.     return vet;
  31.  }
Advertisement
Add Comment
Please, Sign In to add comment