Bucles: 100 primeros números primos

100 primeros primos4.html
#include<stdio.h>

int main(){
    int n,i = 7, count, c;

    printf("\nCuantos numeros primos quiere mostrar :  ");
    scanf("%d", &n);

    if(n >= 1)
    {
        printf("\n\nLos primeros %d numeros primos a partir del %d son:\n", n,i);
        //printf("2 ");
    }

    // iteration for n prime numbers
    // i is the number to be checked in each iteration starting from 7
    for(count = 2; count <= n; i++)  
    {
        // iteration to check c is prime or not
        for(c = 2; c < i; c++)
        {
            if(i%c == 0)
                break;
        }

        if(c == i)  // c is prime
        {
            printf("%d ", i);
            count++;    // increment the count of prime numbers
        }

    }
    printf("\n\n");
    system("pause");
    return 0;
}

No hay comentarios:

Publicar un comentario