Guest User

Untitled

a guest
Jan 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. void verificacaoDePrimos(int* vetor, int* numero)
  6. {
  7.     int i,j,k,n;
  8.     int aux;
  9.     n=*numero;
  10.  
  11.     for(i=0;i<n-2;i++)
  12.     {
  13.         for(j=1;j<n-2;j++)
  14.         {
  15.             if((vetor[i+j])%(vetor[i])==0)
  16.             {
  17.                 for(k=i+j;vetor[k]<vetor[k+1];k++)
  18.                 {
  19.                     aux=*vetork[k];
  20.                     vetor[k]=vetor[k+1];
  21.                     vetor[k+1]=aux;
  22.  
  23.                 }
  24.                 n--;
  25.             }
  26.         }
  27.     }
  28.     n=*numero;
  29.  
  30.     for(i=0;i<n-2;i++)
  31.     {
  32.         printf("%d ",vetor[i]);
  33.     }
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. void formaVetor(int* numero)
  50. {
  51.     int i,valor;
  52.     int* vetor;
  53.     valor=*numero;
  54.  
  55.     vetor=(int*)malloc((valor-1)*sizeof(int));
  56.  
  57.     if(vetor==NULL)
  58.     {
  59.         printf("Nao tem espaco disponivel na memoria!");
  60.         exit(1);
  61.     }
  62.     else
  63.     {
  64.         for(i=2;i<=valor;i++)
  65.         {
  66.             vetor[i-2]=i;
  67.         }
  68.  
  69.         verificacaoDePrimos(vetor,numero);
  70.  
  71.  
  72.  
  73.     }
  74.     free(vetor);
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. int main()
  82. {
  83.     int numero;
  84.  
  85.     do
  86.     {
  87.         printf("Digite o numero e veja todos os primos ate o numero digitado: ");
  88.         scanf("%d",&numero);
  89.  
  90.     }while(numero<=1);
  91.  
  92.     formaVetor(&numero);
  93.  
  94.  
  95.     return 0;
  96. }
Add Comment
Please, Sign In to add comment