Advertisement
Kyrexar

Nº primo (cantidad)

Apr 19th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 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 cuantos primos le pidas (los primeros) */
  17.  
  18. int main(){
  19.     int a=1, b, n, i;
  20.    
  21.     printf(" \n Intoduzca cuantos numeros primos desea: ");
  22.     scanf("%d",&n);
  23.  
  24. /* Aprovechando la función anterior,
  25. comprueba de 1 en 1 todos los numeros por orden
  26. y solo suma 1 al contador si es primo.
  27. El programa se detiene cuando el contador alacanza la cifra introducida*/
  28.    
  29.     i=1;
  30.     while( i<=n ){
  31.        b=primo(a);
  32.        if ( b==1 ){
  33.           printf(" %d",a);
  34.           i++;
  35.        }
  36.        a++;
  37.     }
  38.  
  39.     printf(" \n ");
  40.     system("PAUSE");
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement