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.
#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 */
}
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.
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.
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?