Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.21/5 (5 votes)
See more:
How do I write a program that finds and prints all of the prime numbers between 3-100?
Posted

Try this code sample:

C++
#include
using namespace std;
void prime_num(int);
int main()
{
    cout << " Enter a number and I will generate the prime numbers up to that number: ";
    int num = 0;
    cin >> num;
    prime_num(num);
}

void prime_num( int num)
{
    bool isPrime=true;
    for ( int i = 0; i <= num; i++)
    {
        for ( int j = 2; j <= num; j++)
        {
            if ( i!=j && i % j == 0 )
            {
                isPrime=false;
                break;
            }
        }
       if (isPrime)
       {
           cout <<"Prime:"<< i << endl;
       }
       isPrime=true;
    }
}
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 19-Oct-11 6:26am    
I've moved your code from the comment to your solution. Please post code only inside a question or a solution. Alway to remember to always add pre tags. Otherwise the code will not be formatted properly. Unformatted code is very hard to read and decipher.

Thanks for your cooperation!
epanjohura 20-Oct-11 3:42am    
I'm so sorry, I thought I did use pre tags :S
Espen Harlinn 2-Nov-11 17:05pm    
Prime quality answer :)
You should be able to work this out, just break it down into smaller chunks

1) construct your for loop - don't forget you can increment by a factor of two
2) write print statement
3) See Divisibility rule[^] for the rules to see if a number has any simple factors
4) Check to see if the current number can be divided by any of the discovered primes so far

This should be enough to get you started
 
Share this answer
 
The Sieve of Eratosthenes[^] was always a favorite of mine :).
Another oldy but goodie is the Fermat Primality Test[^]. Since the Fermat Primality Test does have its flaws you might want to resort to this page, which lists other primality tests: Primality Test[^]

Best Regards,

—MRB
 
Share this answer
 
Comments
Richard MacCutchan 17-Oct-11 14:11pm    
I'd like to give you extra points for spelling "Eratosthenes" correctly, but you'll have to make do with a standard 5.
Manfred Rudolf Bihy 19-Oct-11 6:22am    
:)
Google It

The first two links will give you sample code
 
Share this answer
 
v2
Here is an implementation for the Sieve of Eratosthenes, There is actually a better Sieve algorithm, but this one (in contrary) is simple to understand.
C++
bool *eratosthenes(int top)
{
    bool * sieve = new bool[top+1];

    // Initialize values

    sieve[0] = false; sieve[1] = false;
    for(int i=2;i<=top;i++)
        sieve[i] = true;

    /* it is sufficient to scan up to the sqrt, 
       because the inner loop would start at i*i. */
    for(int i=2;i <= sqrt(top); i++)
    {
        /*If i is not prime, the 2nd loop is redundant, because there was already p 
          that would have cancelled out both i and every other possible j. */
        if(!sieve[i])
            continue;

        /*we can start from i*i because i*2, ... i*(i-1) would already have 
          been assigned false in previous iterations by prime div of 2.,i-1. */
        for(int j=i*i;j<=top;j+=i)
            sieve[j] = false; // j isn't prime - i is a factor
    }
    return sieve;
}
 
Share this answer
 
v4
int main()
{
bool isPrime=true;

//Loop through all the numbers 3-100
for(int primeCandidate=3; primeCandidate<=100; primeCandidate++)
{
//Reset isPrime to true
isPrime = true;

//Check the factors
for(int factor=2; factor<primecandidate-1;>{
//If a factor is found, the number is not prime. No reason to check
//the rest of the numbers, so break out of this 'for' loop.
if(primeCandidate%factor == 0)
{
isPrime = false;
break;
}
}

//If the number is prime, print it.
if(isPrime == true)
{
cout<< primeCandidate << " is prime." << endl;
}
}

return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900