sábado, 6 de septiembre de 2014

3. Que rellene un array con los números primos comprendidos entre 1 y 100 y los muestre en pantalla en orden ascendente.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
            int x,cont,z,i,tabla[100];
           
            i=0;
            for (x=1;x<=100;x++)
            {
        cont=0;
        for (z=1;z<=x;z++)
        {
            if (x%z==0)
            {
               cont++;
            }
        }
       
        if (cont==2 || z==1 || z==0)
        {
         tabla[i]=x;
         i++;
        }
       
    }
           
            for (x=0;x<i;x++)
            {
        printf("%d\n",tabla[x]);
    }
           
    system("PAUSE");     
    return 0;

}

4 comentarios:

  1. como se podria hacer ese mismo pero descendente?

    ResponderEliminar
  2. en descentente

    #include
    #include

    int main(void)
    {
    int x,cont,z,i,tabla[100];

    i=0;
    for (x=1;x<=100;x++)
    {
    cont=0;
    for (z=1;z<=x;z++)
    {
    if (x%z==0)
    {
    cont++;
    }
    }

    if (cont==2 || z==1 || z==0)
    {
    tabla[i]=x;
    i++;
    }

    }

    for (x=100;x>=1;x--)
    {
    printf("%d\n",tabla[x]);
    }

    system("PAUSE");
    return 0;
    }

    ResponderEliminar