Prime numbers below 25

Hi all!

New to programing and to this forum. Hoping to get some feedback on my code. I want to find all prime numbers below 25. The code I have compiled currently lists the prime numbers it found repeatedly. Can't understand why...would really appreciate some guidance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;

int main()
{
    int i;
    for(i=2; i<25; i++)
    {
        for(int divisor=2; divisor<i; divisor++)
        {
            if(i%divisor==0)
                break;
            else
            {cout<<i<<endl;}
        }
    }
    
    cin.ignore();
    cin.get();
    return 0;
    
}
Because you're having a number output every time it proves that it isn't evenly divisible by the divisor, not just when it is prime you are also incorrectly testing for primeality. For example, '9' isn't prime but your program will output it when you test '9' against '2', '4', '5','6','7' and '8'. You should run through all of the divisors before you output the number.
Thanks, I changed it a bit to better test for prime numbers using sqrt(i). New code below...however still don't understand how to stop it from adding the same number to the list.

1
2
3
4
5
6
7
8
9
10
11
12
int i;
    for(i=2; i<25; i++)
    {
        for(int divisor=2; divisor<=i; divisor++)
        {
            if(i%divisor==0)
                break;
            else if
            (divisor+1>sqrt(i))
            {cout<<i<<endl;}
        }
    }
Try using a vector and every time you get a new prime number, add it to the vector. Then whenever you output a number, check that the number isn't already in the vector before you output it, if it is, don't output it.
Add a break; after your cout << i < endl;.
With the break; it now outputs just "3 5 7" :/
I meant to put the break; after the cout << i << endl; but before the }.
ah THANK YOU!
Following up on this...is it possible to add each output into an array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{
    int i;
    int array[6];    
    for(i=2; i<=25; i++)
    {
        for(int divisor=2; divisor<=i; divisor++)
        {
            if(i%divisor==0)
                break;
            else if
            (divisor+1>sqrt(i))
            {
                array[i]=i;
                cout<<array[6];}
               break;}        
       }
     
    cin.ignore();
    cin.get();
    return 0;
    
}
    
    
I would prefer std::vector instead if at all possible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::vector<int> primes;
for (int i = 2; i <= 25; ++i)
{
    for (int divisor = 2; divisor <= i; ++divisor)
    {
        if (i % divisor == 0)
            break;
        else if (divisor + 1 > sqrt(i))
        {
            primes.push_back(i);
            std::cout << i << '\n';
            break;
        }
    }
}

But if you must use an array, you'll have to keep track of the last index yourself:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int primes[50]; // Plenty of space, just in case :)
int numPrimes = 0;
for (int i = 2; i <= 25; ++i)
{
    for (int divisor = 2; divisor <= i; ++divisor)
    {
        if (i % divisor == 0)
            break;
        else if (divisor + 1 > sqrt(i))
        {
            primes[numPrimes] = i;
            ++numPrimes; // Or you could just say 'primes[numPrimes++] = i;'
            std::cout << i << '\n';
            break;
        }
    }
}
Using an array...I now want to access the 4th element within it. This doesn't quite work :/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int primes[50]; 
    int numPrimes = 0;
    for (int i = 2; i <= 25; ++i)
    {
    for (int divisor = 2; divisor <= i; ++divisor)
    {
        if (i % divisor == 0)
            break;
        else if (divisor + 1 > sqrt(i))
        {
            primes[numPrimes] = i;
            primes[numPrimes++]=i; 
            cout << primes[4]<<" ";
            break;
        }
    }
Why are you setting 2 elements in the array equal to i each time?

Edit: Nevermind you increment after setting the second time so just take the first one out.
Last edited on
Think I got it...thanks for all the help!
Topic archived. No new replies allowed.