954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help in a beginer c++ program

Hello guys,
I need some help with a program that finds all the prime numbers from 2 to 10000,here is what I have so far

#include <iostream>
using namespace std;
bool prime(int n );




void main ()
{
int n,b;
b=0;
for (n=2;n<=10000;n++)
prime (n);
if ()
{
cout<<n<<endl;
b=b+1;

 }
 cout <<b;
   }
   
bool prime (int n)
{
int i;
for (i=2;i<=100;i++)
{
if(n%i==0)
return false;
}

return true;
}

any help would be much aprreciated ,thanks!

basharg
Newbie Poster
3 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

ok I think this much better at least I get results :P ,I dont know what was I thinking when I was writing the old one,anyways here is the new

#include <iostream>
using namespace std;
int prime(int i );




void main ()
{
int c,z;
for (c=2;c<=10000;c++)
{
z=prime (c);

if (z=1)
cout<<c;
}
}

int prime (int n)
{
int x;
for(x=2;x<=n;x++ )
{if (n%x==0)
return 0;
else
return 1;
}
}

Can you please check if it is correct .Thanks alot

basharg
Newbie Poster
3 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

#include
using std::cout;
using std::endl;
using std::cin;

#include

#include
using std::setw;

bool prime(int n);

int main()
{
int count = 0;

cout << "The prime numbers from 2 to 10000 are:\n";

for (int x =2; x <= 10000; ++x )

if (prime(x))
{
++count;

cout << setw( 6 ) << x;


if ( count % 5 == 0)
cout << endl;
}
cout << "\nThere were " << count << " prime numbers tested\n" << endl;

return 0;
}
bool prime (int n)
{
for ( int z = 2; z<=sqrt(double(n)); z++)

if( n%z == 0 )
return false;

return true;
}

gevorg1808
Newbie Poster
12 posts since Oct 2011
Reputation Points: 3
Solved Threads: 3
 

Thank alot mate ,cheers !!

basharg
Newbie Poster
3 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You