So for my program im trying to make it tell you if a number is prime or not. But for some reason according to my program no numbers over five are prime..... which is simply untrue apparently according to my program 7 is not a prime number... im using xcode btw... so can someone diagnose the problem and explain to me why thats happening and how to fix it.... oh yeah heres the code!

- Ryan

#include <iostream>

#include <cmath>

using namespace std;

int main(int argc, const char * argv[])

{

int n; // Number to test if prime ( Prime-ness of the number)

int i; // loop counter!

int is_prime=true; // Boolean flag, assume true for now

//Get a number for the prime-ness test

cout<<" Enter a number to test the prime-ness of \n";

cin>>n;

// Test for prime-ness by checking for divisibility by all whole numbers from 2 to sqrt(n)

i=2;

while (i <= sqrt(n)) { // while i is less than or equal to sqrt(n)

if (n % 1 == 0) // if i divides n

is_prime=false; // n is not prime

i++; //add 1 to i

}

//Print results

if(is_prime)

cout<<n<<" is a prime number! \n";

else

cout<<n<<" is not a prime number";

return 0;

}

- Ryan

#include <iostream>

#include <cmath>

using namespace std;

int main(int argc, const char * argv[])

{

int n; // Number to test if prime ( Prime-ness of the number)

int i; // loop counter!

int is_prime=true; // Boolean flag, assume true for now

//Get a number for the prime-ness test

cout<<" Enter a number to test the prime-ness of \n";

cin>>n;

// Test for prime-ness by checking for divisibility by all whole numbers from 2 to sqrt(n)

i=2;

while (i <= sqrt(n)) { // while i is less than or equal to sqrt(n)

if (n % 1 == 0) // if i divides n

is_prime=false; // n is not prime

i++; //add 1 to i

}

//Print results

if(is_prime)

cout<<n<<" is a prime number! \n";

else

cout<<n<<" is not a prime number";

return 0;

}

I immediately see two problems.

1) sqrt(n) is ambiguous. I'm surprised this is even compiling for you because it shouldn't. There is no form of sqrt which takes an int. You have to give it either a double, a float, or a long double. If you only have an int, you have to cast it:

2)

1) sqrt(n) is ambiguous. I'm surprised this is even compiling for you because it shouldn't. There is no form of sqrt which takes an int. You have to give it either a double, a float, or a long double. If you only have an int, you have to cast it:

`sqrt( ``static_cast`<`double`>(n) )

2)

`if` (n % 1 == 0)

<- This is ALWAYS true. Remember that % gives you the remainder after division. Any number divided by 1 is going to have a remainder of zero. Probably meant to have 'i' instead of '1' there.
Thanks! haha i dont know why i had one instead of i! I wasreally tired when i was working on this so i guess i wasnt paying as much attention! Anyway the sqrt(n) works fine for me but i know what you mean! Thanks for that correction!

-Ryan

-Ryan

There is no form of sqrt which takes an int. |

There is such form in C++11, and some compilers implemented it ahead of time.

Topic archived. No new replies allowed.