Advertisement
Kyrexar

Nº primo (intervalo)

Apr 19th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* Funcion explicada en http://pastebin.com/LUSthLse */
  5.  
  6. int primo ( int a ){
  7.     int b, p, i=0;
  8.  
  9.     for ( b=a ; b>=1 ; b-- ) if ( a%b==0 ) i++;
  10.     if ( i>2 ) p=0;
  11.     else p=1;
  12.  
  13.     return p;
  14. }
  15.  
  16. /* Programa que te da los primos entre 0 y la cifra indicada */
  17.  
  18. int main(){
  19.     int a, b, i;
  20.    
  21.     printf(" \n Intoduzca hasta que cifra desea numeros primos: ");
  22.     scanf("%d",&a);
  23.  
  24. /* Aprovechando la funcion anterior,
  25. comprueba de 1 en 1 todas las cifras hasta la introducida
  26. y solo imprime el numero si este devuelve que es primo */
  27.  
  28.     for( i=1 ; i<=a ; i++ ){
  29.        b=primo(i);
  30.        if ( b==1 ) printf (" %d",i);
  31.     }
  32.  
  33.     printf(" \n ");
  34.     system("PAUSE");
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement