Thread: Computing prime numbers up to 10000 runtime error

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    3

    Computing prime numbers up to 10000 runtime error

    Code:
    #include <stdio.h>
    int main()
    {
        long int a[100000];
        int i, j, k, sw;
        int l=0;
        for (i = 0; i<100000; i++)
            a[i] = 0;
        for (j = 3; j<10000; j++)
        {
            for (k = 2, sw = 1; k<j; k++)
                if ((j % k) != 0)
                    sw = 0;
            if (sw = 0)
            {
                a[l] = j;
                l++;
            }
        }
        return 0;
    }
    I'm trying to compute prime numbers up to 10000 and store them in the a-array. the algorithm is simple, it checks to see whether j can be divided to any integer between 1 and itself.
    if it wasn't possible to do that it would assign 0 to sw. if sw was 0 it means j is a prime number hence it gets stored in the a-array.
    this doesn't work. I'm guessing it has some logical error.

    I would appreciate it if someone could help me out.
    Last edited by nullbyte0r; 08-13-2016 at 12:32 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Equality is == and assignment is = anywhere in the code. Making a typo can have interesting unintended effects.

    > if (sw = 0)
    This makes sw 0. Since the result of assignment is the value assigned, and 0 means false in C, the code underneath is never executed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-13-2016, 12:22 AM
  2. Print which numbers that are prime number from 2-100? Logical Error.
    By DecoratorFawn82 in forum C++ Programming
    Replies: 4
    Last Post: 12-01-2015, 03:13 PM
  3. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  4. Replies: 2
    Last Post: 12-24-2009, 02:41 AM
  5. prime numbers code compile error
    By Tony654321 in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 10:13 AM

Tags for this Thread