Prime number program

hello my first post here,i am having problem with this program and i am a new to c++ too,my apologies if i posted in wrong section.
Well here is the program i have done so far:
also i am using dev c++ compiler.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main ()
{
int b,a,r=0;
cout<<"input the number you want to test";
cin>>a;
for (b>1;b<a;b++);
{if (a%b==0)
{cout<<a<<"is not prime";
r++;
}
}
if (r==0)
{
cout<<a<< "is prime number";
}
if (r!=0)
{
cout<<a<< "is not prime number";
}
system ("PAUSE");
return 0;
}


Output is
5 is a prime number5 is not a prime number  press any key to continue

any help would be appreciated .
Last edited on
Your for statement is badly formed:

for (b>1;b<a;b++);

The initialisation statement, b>1, does nothing.

The semicolon on the end closes the for statement, making it the equivalent of:

1
2
3
4
for (b>1;b<a;b++)
{
  ;
}

Thus, there is nothing in the loop to execute.
Last edited on
Thank you very much!
I guess i have a lot to learn,anyways my final "working" program was:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main ()
{
int b,a,r=0;
cout<<"input the number you want to test";
cin>>a;
for (b=2;b<a;b++)
{if (a%b==0)
{cout<<a<<"is not prime";
r++;
}
}
if (r==0)
{
cout<<a<< "is a prime number";
}
if (r!=0)
{
cout<<a<< "is not a prime number";
}
system ("PAUSE");
return 0;
}

thanks again.
Last edited on
You're welcome :)
Topic archived. No new replies allowed.