Prime numbers assignment

I have an assignment at the moment that asks me to make a program that can cout "number is a prime number" when I enter a prime number and then "number is not a prime" when I type in a number that isn't a prime.
Just a few weeks ago, I was able to make a program which counted prime numbers from 1 to 10 (with a bit of help), but I got that working once I found my errors.

Basically this program is 100 times more simpler and something isn't ticking in my head....

Anyway, this is what i have so far...

int number;
cout << "Enter a number" << endl;
cin >> number;

{

if(number % 1 == 0) // when the number is divided by 1 and remainder is 0
cout << "prime" << endl;
else if(number % number == 0) // when number divided by itself, remainder is 0
cout << "number is a prime number";

else
cout << "number isn't a prime number";
}

I'm pretty sure my maths is correct since a prime number can only be divided by 1 and itself, thus if it is divided by 1, then none remains in the modulus operator, and if it is divided by itself, same story.

Program compiles (getting there), but whatever number I enter it will output as "prime" regardless.

Where did I go wrong?
Last edited on
All numbers can be divided by itself and one:
4/1 = 4 (remainder) 0
4/4 = 4 (remainder) 0


what makes it prime is that the number can only be divided by one or itself
number % 1 = 0
number % number = 0
number % other_numbers != 0
closed account (z05DSL3A)
A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself.

eg:
No.   Divisors
1      1               Not Prime
2      1,2            Prime
3      1,3            Prime
4      1,2,4         Not Prime

So a Naïve method would be to take an input number n and check for any integer m from 2 to n-1, that divides n. If n is divisible by m then it is not Prime.


----
Edit:
matsom wrote:
4/4 = 4 (remainder) 0
Typo?
Last edited on
Topic archived. No new replies allowed.