Hi everyone,
I have to write a code that finds and prints all prime numbers from 2 to whatever number the user inputs, be it 10 or 10,000. So far what i have is this:

#include <iostream>
using namespace std;
int main(){
int n,p,t,limit;
	cout<<"input the limit:";
	cin>>limit;
	//outer Loop begins
	cout<<"The prime numbers are:"<<endl;
	for(n=2;n<=limit;n++){//begin outer loop
		//check number for prime number
		for(p=1;p<=n/2;p++){//inner loop
		if (n%p!=0)
			t=n;
		}//end of inner loop
		if(t=n)
		cout<<t<<endl;
	}//end of outer loop
}

the only problem is that so far, it only counts the numbers from 2 to what was inputed. Could you help me fix this? Please keep in mind this is the first class I have ever taken that deals with this subject matter, so i wont understand something that's above my level.

>if(t=n)
Note that in C++ the == operator is for comparison, = is for assignment.

I redid that part, and initiated t=1 in the second for loop. It prints the prime numbers 5 and 7, but then it prints 8 through the inputed number.
What i think is wrong is that the if statement (n%p!=0) stops as soon as p comes upon a number that satisfies it, instead of running through all the numbers until p=n/2. What do i do to fix this?

- p should start from 2
-inner loop should break if remainder == 0, else it is a prime no.

how do i insert the break? I cant seem to find how to do it in the book i bought.

I inserted the break so the code looks like this now:

#include <iostream>
using namespace std;
int main(){
int n,p,t,limit;
	cout<<"input the limit:";
	cin>>limit;
	//outer Loop begins
	cout<<"The prime numbers are:"<<endl;
	for(n=2;n<=limit;n++){//begin outer loop
		//check number for prime number
		for(p=2,t=1;p<=n/2;p++){//inner loop
		if (n%p!=0)
			t=n;
		else 
			break;
		}//end of inner loop
		if(t==n)
		cout<<t<<endl;
	}//end of outer loop
}

The only problem is that it prints out all odd numbers now, instead of just the prime numbers. Can someone tell me what I'm doing wrong?

for(n=2;n<=limit;n++)
{
    t=0;
    for(p=2;p<=n/2;p++)
   {
        if (n%p==0)
        {
             t=1;
             break;
        }
    }
    if(!t) cout<<n<<endl;
}

Thank you everyone, you were a tremendous help. If it had not been for your help i probably would still be stuck trying to get the code to work.

have you tried running his program yet? thanks you really solved my problem, though I really hate asking for help I admit my frailties,, hehe....thanks a lot.

maybe you can add:   if(t==n && t==1){
                                       cout<<t;

just a thought....

have you tried running his program yet? thanks you really solved my problem, though I really hate asking for help I admit my frailties,, hehe....thanks a lot.

maybe you can add: if(t=n && t=1){
cout<<t;

just a thought....

maybe the first one you did, was it! you just need to add a few touch ups...

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.