3

I am having a problem with this code. Although it should say that this number is not prime, as i put a ridiculously large number that i know to be nonprime (252345435465, or even 1000000000000 as an example), it states that the number is prime.

#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

int main() {
int n;
int i;
int prime = true;

cout << "Type in a number and press ENTER: ";
cin >> n;

i = 2;
while (i <= sqrt(n)) {
    if (n % i == 0) {
        prime = false;
        break;
    }
    i++;
}

if (prime)
    cout << "The number is prime" << endl;
else
    cout << "The number is NOT prime" << endl;

system("PAUSE");
    return 0;
}

Is there something I'm doing wrong?

3 Answers3

2

The values you are putting in are too big for an int to hold. They are overflowing the 32 bit limit of a signed int (-2147483648 through 2147483647).

edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
  • 2
    Note that the limits shown above are for a particular C++ implementation. Windows C++ compilers, even 64-bit, will have these limits. Other compilers may have larger or smaller limits (the *minimum* limits for `int` are -32.767 to +32.767, inclusive, IIRC, for 16-bit sign-and-magnitude representation). – Cheers and hth. - Alf Dec 31 '13 at 03:42
2

First, to avoid including the non-standard header <stdafx.h>, just turn off precompiled headers in your Visual C++ project (right click the project, then properties).

The main problem is that you're inputting values too large for the int type.

To detect that, simply check the state of the stream after the input operation:

#include <iostream>
#include <stdlib.h>  // EXIT_FAILURE
#include <cmath>

using namespace std;

int main() {
int n;
int i;
int prime = true;

cout << "Type in a number and press ENTER: ";
cin >> n;
if( cin.fail() )
{
    cout << "Sorry, that's not a valid `int` number." << endl;
    return EXIT_FAILURE;
}

i = 2;
while (i <= sqrt(n)) {
    if (n % i == 0) {
        prime = false;
        break;
    }
    i++;
}

if (prime)
    cout << "The number is prime" << endl;
else
    cout << "The number is NOT prime" << endl;

system("PAUSE");
    return 0;
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • There's actually nothing non-standard about `stdafx.h`. It's just another header, as far as standard C++ is concerned. Of course, using any header violates the idea of a self-contained example. – MSalters Dec 31 '13 at 08:00
  • @MSalters: There are two non-standard issues. First, the most obvious, it's not a header defined by the C++ standard. This is what's usually meant by **non-standard header**, and is the sense in which I used that term. Secoondly, its purpose is to support Microsoft precompiled headers (it is "the" precompiled header in a typical Visual Studio project), which defines a language subtly different from standard C++, so that some valid C++ standard code does not compile (novices are often baffled by this), and so that some invalid C++ code does compile. – Cheers and hth. - Alf Dec 31 '13 at 08:13
1

A 32-bit signed int can take a range of values between –2,147,483,648 to 2,147,483,647. Your number is too large for that range.

Either use a more suitable variable type, or only input numbers which are within range for the variable you are using.

See this answer for more information on C++ data types and their ranges.

Community
  • 1
  • 1
JBentley
  • 6,099
  • 5
  • 37
  • 72