Search
 
SCRIPT & CODE EXAMPLE
 

CPP

check prime number c++

bool prime(int a) { 
    if (a < 2)
        return false;
    for (int i = 2; i * i <= a; i++) {
        if (a % i == 0)
            return false;
    }
    return true;
}
Comment

prime number program in c c++

bool isPrime(int x = 0)
{
    /* Without extra 'count' variable */
    //! corner case: 0 and 1 aren't prime numbers
    if (x == 0 || x == 1)
        return 0;

    for (int i = 2; i <= x / 2; i++)
    {
        if (x > 2 && x % i == 0)
            return 0;
    }

    return 1;
}
Comment

prime number in c++

#include<iostream>
using namespace std;
 
int main()
{
	int n;
	bool c=1;
	cin >> n;
	if (n <= 1)
	{
		cout << "NO
";
	}
	else
	{
		for (int i = 2; i < n; i++)
		{
			if (n % i == 0)
			{
				c=0;
				break;
			}
		}
		if (!c)
			cout << "NO
";
		else
			cout << "YES
";
	}
	return 0;
}
Comment

c++ program to find prime number using function

#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";
	}
}
Comment

find primes in cpp

#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>

#define N 1000000	//N is the Range (0..N)

bitset < N+1 > numbers;
vector < int > primes;

void sieve(){
    numbers.set();
    numbers[1] = 0;

    for (int i = 2; i < N; i++){
        if (numbers[i] == 1){
            cout<<i<<endl;
            primes.push_back(i);
            for (int j = i*i; j<=N; j+=i)
                numbers[j] = 0;
        }
    }
}
Comment

prime number c++


# Prime number:

n = int(input("Please enter your input number: "))
if n>1:
    for i in range(2,n):
        if n%i == 0:
            print("%d is a Not Prime."%n)
            break
    else:
        print("%d is a Prime."%n)
else:
    print("%d is a Not Prime."%n)
    
# Use to Definition Function:

'''
def Prime_number_chcek(n):
    if n>1:
        for i in range(2,n):
            if n%i == 0:
                return ("%d is a Not Prime."%n)
        return ("%d is a Prime."%n)
    return ("%d is a Not Prime."%n)

# Main Driver:
if __name__=="__main__":
    n = int(input("Enter your input number: "))
    print(Prime_number_chcek(n))
'''
Comment

find prime number c++

#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;
}
Comment

prime number c++

bool prime(int x) {
	if (x == 2 || x == 3) return true;
	if (x == 1 || x % 2 == 0 || x % 3 == 0) return false;
	int k = -1;
	while (k<=int(sqrt(x))) {
      	k+=6;
		if (x % k == 0 || x % (k+2) == 0) break;
	}
	return k>int(sqrt(x));
}
Comment

prime number c++

template<class T> bool isPrime(T n) 
{ 
	T i;
  	if(i<2) return false;
	for(i = 2; i * i <= n; i++) { 
    	if(n % i == 0)  return false; 
    } 
	return true; 
}
Comment

C++ prime number

// 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
" : cout << " false
";
    isPrime(15) ? cout << " true
" : cout << " false
";
    return 0;
}
Comment

prime number program c++

#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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: change to lowercase in notepad++ 
Cpp :: max function in c++ 
Cpp :: cpp case 
Cpp :: sort vector in descending order 
Cpp :: c++ string comparison 
Cpp :: how to convert int to std::string 
Cpp :: c++ typedef 
Cpp :: switch case c++ 
Cpp :: string to vector char c++ 
Cpp :: adding element in vector c++ 
Cpp :: sleep system function linux c++ 
Cpp :: do while loop c++ loops continuously 
Cpp :: const char to string 
Cpp :: print each number of digit c++ 
Cpp :: find primes in cpp 
Cpp :: how to scan array in c++ 
Cpp :: minimum value in array using c++ 
Cpp :: play audio c++ 
Cpp :: c++ vector move element to front 
Cpp :: pop_back 
Cpp :: joins in mysql use sequelize 
Cpp :: Pyramid pattren program in C++ 
Cpp :: stringstream stream number to string 
Cpp :: c++ cast to type of variable 
Cpp :: remove specific element from vector c++ 
Cpp :: See Compilation Time in c++ Program 
Cpp :: memory leak in cpp 
Cpp :: Sort html elements in Jquery on condition 
Cpp :: factorial calculator c++ 
Cpp :: iterate vector c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =