Need help with a program

I'm having trouble with a problem for school. I know that I should do this myself so that I will remember it better, but I am having a difficult time with it.

I need to write a program that will output a list of all the prime numbers between 1 and x, including 1 and x and x is any number given by the user, using a nested loop.

Can anyone give me some help with this?

thanks in advance!
I saw such an assignment already. Try to find it here.
This is flawed, 1 is not a prime number. Are you having trouble with the nested loop or the math involved?
The program is just supposed to list all of the prime numbers between 1 and the number given by the user.

I'm having problems with the nested loop.
Something like this (untested + incomplete):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for(int i = 0; /*CONDITION TO BE MET*/; ++i) 
{
     bool isPrime = true;

     for(int j = 0; /*CONDITION TO BE MET*/; ++j)
     {
         if(NUMBER_ENTERED % j == 0) //If At Any Time A Number Between 0 and NUMBER_ENTERED Has No Remainder Then It Is Not Prime
         {
             isPrime = false;
         }
     }

     if(isPrime)
     {
         std::cout << NUMBER_ENTERED << " ";
     }
}


This is probably more help then I should have given you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

bool isPrime(int x) {
    for (int i = 2; i < x; i++) {
        if (x%i == 0) {
            return false;
        }
    }
    return true;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    for (int i = 2; i <= n; i++) {
        if (isPrime(i)) {
            cout << i << endl;
        }
    }
}
Last edited on
@OP: LimeOats did not output the 1 or the 'x', in case you didn't spot it. I would hope you know how to do that part though.
thank you both! very fast responses. I will look over these and learn how to do it.
I thought 1 wasn't a prime number o_o

And @cppnewguy, good. I'm glad you're going to take my program and learn from it, rather than just use it for a grade. I was going to mention that but I forgot. Glad to know you're actually interested in learning.
It isn't but the OP's instructions said explicitly to output 1. I can only imagine that the professor is a bitter individual who has been forced to do things in projects that don't make any logical sense and now they are trying to bludgeon that feeling of complacency to their students.
i created the program for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>


using namespace std;

int main()
{
int x;
cout<<"Give a number:";
cin>>x;
cout<<"The numbers are:\n";
for(int i=1;i<=x;i++)
{
    cout<<i<<endl;
}

}

tell me if i helped.
The OP only wants Prime numbers.
I am sorry i didnt understand.
so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>


using namespace std;

int main()
{
int x;
cout<<"Give a number:";
cin>>x;
cout<<"The numbers are:\n";
for(int i=1;i<=x;i++)
{
    if(i%2==0)
    {
        continue;
    }
    cout<<i<<endl;

}

}

Now tell me if i helped.
1 isn't a prime number, but that doesn't make "The prime numbers between 1 and 100" an invalid statement. Just like "The even numbers between 3 and 9".

But if some one said "tell me the prime numbers between 1 and 7", I don't think I would say 7, it isn't between :)
That would only output odd numbers, not all odd numbers are prime. For example 9 is an odd number that is also a square.
Last edited on
@ skarla: Not quite. Your program finds EVEN numbers not PRIME numbers.

The OP already has an answer given by LimeOats above. Check it out after you read this: http://en.wikipedia.org/wiki/Prime_number

Please remember that these posts are often homework problems (like this one). It is not acceptable on this forum to give out answers as whole code. You need to help the person learn how to do it themselves. I know you want to show off your coding skills but in the long run doing someones homework for them will end up with them failing their class when they can't pass their tests.

Keep working on the prime number code though, it's a good practice problem!

EDIT: Oop, missed the continue, Computergeek is right, odd numbers.
Last edited on
I find odd numbers like you said in the end.
I am sorry man i just want help others not to confuse them.
Topic archived. No new replies allowed.