#include <iostream>
#include <vector>
#include <limits>
usingnamespace 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 (unsignedint 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. ";}
elseif (v[i] == 3){cout << v[i] << " is a prime number. ";}
elseif (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;
}
#include <iostream>
#include <vector>
#include <limits>
usingnamespace 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;
}
elseif (input == 3){
cout << input << " is a prime number. ";
c++;
break;
}
elseif (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.