ramontricolor12

LISTA 08 - Exercício 13

Aug 9th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. /*CABEÇALHO
  2.     Arquivo: LISTA 08 - Exercício 13.c
  3.     Objetivo: Desenvolver uma lista crescente de números primos.
  4.     Autor(a): Ramon Borges.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. //PROTÓTIPO DE FUNÇÕES
  10. int* criaPrimos(int qtde);
  11.  
  12. int main()
  13. {
  14.     int qtde, *numeros, i;
  15.     printf("Informe a quantidade exata de numeros primos que deseja: ");
  16.     scanf("%d", &qtde);
  17.     numeros = criaPrimos(qtde);
  18.  
  19.     for(i = 0; i < qtde; i++)
  20.         printf("%d ", *(numeros+i));
  21.     free(numeros);
  22.     return 0;
  23. }
  24.  
  25. int* criaPrimos(int qtde)
  26. {
  27.     /*Definição:
  28.        >>Números primos são os números naturais que têm apenas dois divisores diferentes: o 1 e ele mesmo.*/
  29.     int *p, geradorNum, numeroPrimos;
  30.  
  31.     geradorNum = 2;
  32.     numeroPrimos = 0;
  33.     p = (int*) malloc(qtde*sizeof(int));
  34.     while(numeroPrimos < qtde)
  35.     {
  36.         if(geradorNum == 2 || geradorNum == 3 || geradorNum == 5 || geradorNum == 7)
  37.         {
  38.             p[numeroPrimos] = geradorNum;
  39.             numeroPrimos++;
  40.         }
  41.         else if(geradorNum % 2 != 0 && geradorNum % 3 != 0 && geradorNum % 5 != 0 && geradorNum % 7 != 0)
  42.         {
  43.             p[numeroPrimos] = geradorNum;
  44.             numeroPrimos++;
  45.         }
  46.         geradorNum++;
  47.     }
  48.     return p;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment