Prime numbers are not listed

-1

I'm trying to create a C program that shows all prime numbers from 1 to 100 using brute force, but my program shows nothing on the screen.

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

int main()
{
    int x=100,n=2,z=0;
    int p[z];
    verif:
    while(x!=1)
    {
        while(x%n!=0)
        {
            if(n==(x-1))
            {
                z++;
                p[z-1]=x;
                break;
            }
            n++;
        }
        x--;
        n=2;
        goto verif;
    }
    while(z>=0)
    {
        printf("%d",p[z]);
        z--;
    }
}
    
asked by anonymous 17.11.2018 / 20:22

1 answer

2

The biggest problem is that your array has zero elements, I think you wanted to work with 100 of them.

Let's simplify the code?

#include <stdio.h>

int main() {
    int p[100];
    int z = 0;
    for (int x = 100; x > 1; x--) {
        for (int n = 2; x % n != 0; n++) {
            if (n == x - 1) {
                p[z++] = x;
                break;
            }
        }
    }
    while (z >= 0) printf("%d ", p[z--]);
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

    
18.11.2018 / 00:58