Prime numbers problem
I have no idea why this program of mine doesn't work.
using namespace std;
int a, nr, n;
for(int i=1; i<=n; i++)
if(a%2!=0 && a%3!=0 && a%5!=0 && a%7!=0)
cout<<"all the numbers are prime.";
cout<<"all the numbers are not prime.";
It's supposed to input a set amount of numbers, then tell you if all of them are prime or not.
Also excuse me for my bad english I'm from Sweden.
Hej, har du sett min häst?
Han är grön och liten ;_;
you didn't initialize nr.
int a, nr=0, n;
I did what you said, but it still doesn`t work... It tells me that all the numbers are not prime even if i input only prime numbers..
Well, your test at line 12 is not the proper way to test for prime numbers.
Let's say you input a = 3.
a%3 is the remainder of 3/3. The remainder is 0. 0!=0 so that will evaluate to false. false && anything is false, therefore you will not enter the if statement and nr will not be incremented.
do something like:
for (int i = 2; i < sqrt(a); ++i)
a is not prime;
a will be prime if not broken in the if in any iteration;
For a number to be prime, it can't be divisible by any number less than itself, besides 1. You're only testing divisibility by 2,3,5, and 7.
Topic archived. No new replies allowed.