# 8+ CPP Prime Number Checker Program Examples

This post contains a total of 8+ Hand-Picked C++ Prime number checker Program Examples with Source Code. All these prime number checker programs are made using C++ Programming Language.

You can use the source code of these examples with credits to the original owner.

## C++ Prime Number Checker Programs

### 1. By Hatsy Rei

Made by Hatsy Rei. Simple C++ Program to check if a number is prime or not. Source

```10
10 is prime : false
(Can be divided by 2)```
``````#include <iostream>
int main()
{
try
{
unsigned long long int x, y;

std::cin >> std::ws;
if (char(std::cin.peek()) == '-') throw 1;

std::cin >> x;
if (std::cin.fail()) throw 0;

for (y = x%2?3:2; y*y<=x; y+=2)
if (!(x%y)) break;

std::cout << std::boolalpha << x << " is prime : " << (((x%y)||(x==y))&&(!(x<2))) << std::endl;

if (!(x%y)&&(!(x<4)))
std::cout << "(Can be divided by " << y << ")";
}
catch (int err)
{
std::cout << (err ? "-ve values not allowed." : "Invalid input. D:");
}
return 0;
}``````

### 2. By Jully Fredy

Made by Jully Fredy. Basic Prime number checker program. Source

```Enter a positive integer: 12
You have entered: 12
12 is not a prime number```
``````#include <iostream>
using namespace std;
int main(){
int n, i;
bool isPrime = true;

cout << "Enter a positive integer: \n";
cin >> n;

for(i = 2; i <= n / 2; ++i){
if(n % i == 0){
isPrime = false;
break;
}
}
cout << "You have entered: "<<n<<"\n";
if (isPrime)
cout << n <<" is a prime number";
else
cout << n << " is not a prime number";
return 0;
}
``````

### 3. By AliReza

```input positive integer number
7
7 is prime number```
``````#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
cout<<"input positive integer number\n"<<n<<" is ";
if(n==2) cout<<"prime number";
else if(n==1||n%2==0) cout<<"not prime number";
else
{
for (int a=n-1;;a--)
{
if(a==1){cout<<"prime number"; break;}
if(n%a==0){cout<<"not prime number"; break;}
}
}
return 0;
}``````

### 4. By Angelin Blessy

Made by Angelin Blessy. C++ Prime number checker program made using constructor. Source

```Enter a number:34
34 is not a prime number```
``````#include <iostream>
using namespace std;
class prime
{
int a,k,i;
public:
prime(int x)
{
a=x;
}
void calculate()
{
k=1;
{
for(i=2;i<=a/2;i++)
if(a%i==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout<<a<<" is a prime number";
else
cout<<a<<" is not a prime number";
}

};
int main()
{
int a;
cout<<"Enter a number:";
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
return 0;
}``````

### 5. By Chris

```enter a number
44
its not a prime number```
``````#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"enter a number"<<endl;
cin>>num;

if(num <= 1)
cout<<"its not a prime number";
if(num == 2)
cout<<"its a prime number";

for(int x = 2; x < num; x++)
{
if(num % x == 0)
{
cout<<"its not a prime number";
break;
}
if(x == num-1)
cout<<"its a prime number";
}

return 0;
}``````

### 6. By Ayush Sinha

Made by Ayush Sinha. Basic prime number checker program. Source

```33
It is not a prime number,in fact it is a composite number ```
``````#include <iostream>
using namespace std;
string prime_checker(unsigned x){
if(x<=1) return "It is neither a prime nor a composite number";
for(unsigned i=2;i<x;i++){
if (x%i==0) return "It is not a prime number,in fact it is a composite number ";
}
return "True,it is a prime number";
}
int main() {
unsigned n;
cin>>n;
cout<<prime_checker(n);
return 0;
}``````

### 7. By Ankit Saxena

Made by Ankit Saxena. Program to Check if a number is prime number using flag variable. Source

```Enter a positive integer: 50
This is not a prime number```
``````#include <iostream>
using namespace std;
int main()
{
int n, i, flag;
flag = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(i = 2; i <= n / 2; ++i)
{ if(n % i == 0)
{ flag = 0;
break; }
}
if (flag==1)
cout << "This is a prime number";
else cout << "This is not a prime number";
return 0; }``````

### 8. By Sudeep Jaiswal

Made by Sudeep Jaiswal. This C++ Program displays all the prime numbers in a given range. Like if you entered 100 then you will get all the prime numbers that come before 100. Source

```10
The Prime Numbers Are:
2
3
5
7
```
``````#include <iostream>
using namespace std;
int main() {
int num,i,flag,n;
cin>>n;
cout<<"The Prime Numbers Are: ";
for(num=1; num<=n; num++){
flag=0;
for(i=2;i<=num/2;i++){
if(num%i==0){
flag=1;
break;
}
}
if(flag==0 && num!=1)
cout<<endl<<num;
}
cout<<endl<<"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tCreated By Sudeep Jaiswal"<<endl<<"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tPlease Hit A Upvote";
return 0;
}``````

### 9. By Kai

```10
10 is not prime number

thank you ```
``````#include <iostream>
using namespace std;

int main() {
int n,i;
int c=0;
cin>>n;
for( i=1;i<=n;i++)
{
if (n%i==0)
c++;
}
if(c==2){
cout<<n<<" is prime number";
}
else {
cout<<n<<" is not prime number";
}
cout<<"\n\n\nthank you ";

return 0;
}``````