Can not get prime function to work..help please

Hello!

This program is suppose to get a whole number input from the user and say weather that number is prim or not. I can not get this program to work properly. If I put in the number 6 the program tells me it is a prime number. It does this for every number. I can not figure out what is wrong. I was hoping someone could help me find the problem with my code.

This is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

#define PROMPT "Please enter a whole number:"
#define NOT_PRIME "The number is not a prime number./n"
#define PRIME "The number is a prime number./n"
#define DONE 0 /* ends successful program */
#define FIRST_FACTOR 2 /* initial value in for loop */

using std::cout;
using std::cin;
using std::endl;

int main(){
int i; /* loop counter */
int number; /* number provided by user */

cout << PROMPT; /* promt user */
cin >> number; /* wait for user input */

/* Prime numbers are defined as any number
* greater than one that is only divisible
* by one and itself. Dividing the number
* by two shortens the time it takes to
* complete. */

for(i = FIRST_FACTOR; i < number/2; ++i)
if( number % i == 0 ){ /* if divisible */
cout << NOT_PRIME << number; /* not prime */
return DONE; /* exit program */
}

/* if number is not divisible by anything
* than it must be prime */
cout << PRIME << number;
return 0; /* exit program */
}


Please help! thank you.
Last edited on
line 26 should be if( number % i == 0), you are using the division operator instead of mod.
I changed that but the program still gives me the incorrect answer. It will ask to put in a whole number and it will say it is prime when it is not.

example: I input 6 and the program tells me it is a prime number but it isn't. Can anyone help me find the problem?
char number;
should be
int number;

FIRST_FACTOR should be 2
I fixed that but it still is saying that 6 is a prime number.
Can you post the latest version of your code, please.
I just updated the code above to the latest code I have. Still having problems with it.
Line 15:
char number;
should be
int number;

This error hasn't been fixed yet?
Yes it has, I forget that when I edited this code on here but I am gonna take the code I have now and put it on here now. The above code is now accurate.
It works ok for me.
(though the "/n" should be "\n" - but doesn't affect the result)
So when you input the number 6, it says it is not a prime number?
Yes. Just one question. You changed the code, but did you recompile? Depending on the IDE you might be running an outdated version which came from a previous compile.
haha yes I compiled, I see what happened. When I went to fix the error for the FIRST_FACTOR with the 2 instead of 3 I forgot to change the number on the inside of the code. My mistake and a stupid one ha

Thank you for your help, code works perfect! It made sence to me that it should work and I finally saw that little error that let it work right.

Thank you! sorry for the annoying confusion on my part.
*sense
Although it does say that 4 is a prime number, but after 4 every number i put in tells me it is prime and not prime correctly. Why wont it work for the #4?
because first factor is 2 and 2 is not smaller than 4/2, fix:
for(i = FIRST_FACTOR; i <= number/2; ++i)

or you can use do-while loop to eliminate that extra check.
Last edited on
Got it!! Thank you!!
Topic archived. No new replies allowed.