Hi techies,
I am new into C++. I like to write a program that generates all the primes number between 1 and 100. Below is the code I have written but brings no output.
Thanks for any help...

//A program that generates all of the prime numbers between 1 and 100

#include <iostream>

using namespace std;

int main()
{
    int n;

    for(n=1; n>=100; n++)
    {
        bool isPrime = true;

        for(int i=2; i<n; i++)
        {
            if (n%i==0)
            {
                isPrime = false;
                break;
            }
        }
        if(isPrime)
        {
            cout<<n <<" is Prime";
        }
    }

    return 0;
}

Recommended Answers

All 3 Replies

Your problem is that this for loop never runs. You have the greater than/less than sign mixed up.
change

for(n=1; n>=100; n++)

to

for(n=1; n<=100; n++)

As the first replier said, you've got the inequality signs all wrong. Change your code to this:

#include <iostream>

using namespace std;

int main()
{
    int n;

    for(n=1; n<101; n++)//or n<=100
    {
        bool isPrime = true;

        for(int i=2; i<n; i++)
        {
            if (n%i==0)
            {
                isPrime = false;
                break;
            }
        }
        if(isPrime)
        {
            cout<<n <<" is Prime";
        }
    }

    return 0;
}

Thank you for helping me out. It works...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.