xxxxxxxxxx
#include<iostream>
using namespace std;
bool isPrimrNumber(int number){
bool isPrimeFlag=true;
for(int i=2;i<number;i++){
if(number%i==0){
isPrimeFlag=false;
break;
}
}
return isPrimeFlag;
}
int main()
{
int number;
cout<<"Please enter your number";
cin>>number;
bool isPrimeFlag=isPrimeNumber(number)
if(isPrimeFlag)
{
cout<<"prime Number";
}
else
{
cout<<"not prime number";
}
}
xxxxxxxxxx
#include<iostream>
using namespace std;
int main()
{
int n;
bool c=1;
cin >> n;
if (n <= 1)
{
cout << "NO\n";
}
else
{
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
c=0;
break;
}
}
if (!c)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}
xxxxxxxxxx
#include <math.h>
// time: O(sqrt(n)) .. space: O(1)
bool isPrime(int n) {
if (n < 2) return false;
int iter = 2;
while(iter <= sqrt(n)) {
if (n % iter == 0) return false;
iter++;
}
return true;
}
xxxxxxxxxx
#include <iostream>
using namespace std;
int main()
{
int n, i;
cout << "Enter any number :";
cin >> n;
if (n % 2 != 0 && n % n == 0 && n % 1 == 0)
{
cout << "prime number";
}
else
{
cout << "not prime";
}
return 0;
}
xxxxxxxxxx
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=2;i<sqrt(n)+1;i++){
if(n%i==0){
cout<<"number is not a prime";
return 0;
}
}
cout<<"number is a prime";
return 0;
}
xxxxxxxxxx
// A optimized school method based C++ program to check
// if a number is prime
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Driver Program to test above function
int main()
{
isPrime(11) ? cout << " true\n" : cout << " false\n";
isPrime(15) ? cout << " true\n" : cout << " false\n";
return 0;
}
xxxxxxxxxx
#include <iostream>
using namespace std;
int main() {
int i, n;
bool is_prime = true;
cout << "Enter a positive integer: ";
cin >> n;
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
is_prime = false;
}
// loop to check if n is prime
for (i = 2; i <= n/2; ++i) {
if (n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number";
return 0;
}