### Suggestions on improving my working program

I have a program done which works, but for some reason it seems like it could be more concise. I would love any suggestions as to how to make this better. Also, I am getting the following warning:

"39 [Warning] converting to `int' from `double' "

I am curious if this warning is due to a syntax error. If so what would be the best way to fix it. Thanks in advance for any suggestions. The scope of the program is to have a user enter a positive integer and tell the user whether the integer is prime or not.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include using namespace std; int main() { int num; int sqrtNum; cout << "Enter a positive integer: "; cin >> num; cout << endl; while (num <= 1){ cout << "You entered an invalid number.\n" << "Remember, we want a positive number greater than 1.\n" << "Try again: "; cin >> num; cout << endl; } // EVEN NUMBER if (num % 2 == 0){ if( num == 2) cout << "Number " << num << " is prime." << endl; else cout << "Number " << num << " is not prime." << endl; } // ODD NUMBER else{ sqrtNum = sqrt(num); if (sqrtNum % 2 == 0) // If int of sqrt is even, subtract 1 sqrtNum -= 1; while (sqrtNum >= 1){ if (num <= 7 || sqrtNum < 3){ cout << "Number " << num << " is prime." << endl; break; } if (num % sqrtNum == 0){ cout << "Number " << num << " is not prime." << endl; break; } sqrtNum -= 2; } } system ("PAUSE"); return 0; }``````
Just means your compiler is doing an implicit conversion. Isn't a big deal going the way it is, but if it were double to int, that would be an issue. Could fix it by changing sqrt to double
@ResidentBiscuit - It is double to int. :-)

Basically, the sqrt function returns a double value and you're storing it in an integer. That means that anything after the decimal point will be truncated, potentially leading to unwanted answers.

As ResidentBiscuit says, the fix is to make the value you're holding a result in a double, presuming you want an accurate representation of the square root result. If you did just want to retain an integer value and not have the warning, you'd just need to explicitly cast the data type.
Last edited on
Ah looks like I can't read haha. This is why I shouldn't post that late at night :)
Thanks for the explanation. I believe i will leave it the way it is then. It works for me and i need to divide by an integer anyway when i look for primeness. Glad to see all these great minds looking to help out us newbies looking to get into this field.
System pause is evil! try and break the habit of using that before it's to late.
Is 1 not a positive integer?
Topic archived. No new replies allowed.