Check for Prime Numbers - C++

Personal Contribution.

Just a quick question, 1 is not considered prime number, right ?

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
#include <iostream>
#include <vector>
#include <limits>
using namespace std;

void prime_num(int imax){   // Checks for prime numbers
    
    vector <int> v; 
    
    cout << "Enter a positive integer value less than integer maximum value 2147483647: ";
    int input;
    cin >> input;
    v.push_back(input);
    
    cout << endl;
    
    for (unsigned int i = 0; i < v.size(); i++) {
        if(v[i] >= imax){
            cout << " You have reached the maximum value of int!";
            break;
        }
        if(v[i] < 0){cout << " Only input positive integer numbers!" << endl;}
        if (v[i] == 2){cout << v[i] << " is a prime number. ";}
        else if (v[i] == 3){cout << v[i] << " is a prime number. ";}
        else if (v[i]!= 1 && v[i]%2 != 0 && v[i]%3 != 0){cout << v[i] << " is a prime number. ";}
        else{
            cout << v[i] << " is not a prime bumber.\n";
        }
    }
}
int main(){
    int imax = std::numeric_limits<int>::max();   // imax = 2147483647
    prime_num(imax);
    return 0;
}
Last edited on
cpp.sh website is down right now, and I cannot test this code.

I think it should be right. The only thing is that it gives you another chance if you do not enter a prime number.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <limits>
using namespace std;

void prime_num(int imax){   // Checks for prime numbers

    int c = 0;
    while(c < 1){
        
        cout << "Enter a positive integer value less than integer maximum value 2147483647: ";
        int input;
        cin >> input;
        cout << endl;
    
        if(input >= imax){
            cout << " You have reached the maximum value of int!";
            // break;
            c--;
        }
        if(input < 0){
            cout << " Only input positive integer numbers!" << endl;
            c--;
        }
        if (input == 2)
        {cout << input << " is a prime number. ";
        c++;
        break;
        }
        else if (input == 3){
            cout << input << " is a prime number. ";
            c++;
            break;
        }
        else if (input!= 1&& input%2 != 0 && input%3 != 0){
            cout << input << " is a prime number. ";
            c++;
            break;
        }
        else{
            cout << input << " is not a prime bumber.\n";
            c--;
        }
    }
}
int main(){
    int imax = std::numeric_limits<int>::max();   // imax = 2147483647
    prime_num(imax);
    return 0;
}
1 is not considered a prime number.
Btw., why pass imax as an argument, especially since you have already hard-coded it into the string? And, since it's a maximum number, checking if input > imax doesn't do anything, since input just physically cannot be larger.

And I didn't test the code, but looking at it I think it's wrong, a prime number has 2 divisors, 1 and itself. You need to check in a loop from 2 to square root of input if input is divisible by anything more than 1 and itself

Edit: try 35, it says it's a prime number, but it's 5*7.
Last edited on
@TheHardew, right on! Both sample codeshave this bug, I have to fix it.

I didn't get any help from the internet, I tried to write the code by myself, so I guess that is why it contains some errors.
Topic archived. No new replies allowed.