+3 votes
18 views

A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Prime number is always a Positive Integer. For Example: 3,5,7,11,13 etc.

Here is how to find Prime numbers in C++

using namespace std; 



bool isPrime(int n) 
{ 

    // Check if given nmbr is less than or equal to 1 (prime nmbr is always greater than 1)
    if (n <= 1) 
        return false; 



    // Check from 2 to n-1 

    for (int i = 2; i < n; i++) 

        if (n % i == 0) 

            return false; 

    return true; 
}

int main() 
{ 

    isPrime(11) ? cout << "Prime\n" : cout << "Not Prime\n"; 

    isPrime(7) ? cout << " Prime\n" : cout << "Not Prime\n"; 

    return 0; 
} 
by (2.5k points)   | 18 views

Please log in or register to answer this question.

Related questions

+1 vote
1 answer
+1 vote
1 answer
asked Jun 27 by fatyma Nauman (485 points) | 14 views
+2 votes
0 answers
asked Jun 12 by toheed (1.1k points) | 8 views
+2 votes
2 answers
asked Jun 4 by toheed (1.1k points) | 68 views
+2 votes
1 answer
+2 votes
1 answer
asked Jun 11 by noor (1.1k points) | 8 views
+1 vote
1 answer
asked Jun 11 by noor (1.1k points) | 8 views
+1 vote
0 answers
asked Jun 5 by toheed (1.1k points) | 9 views




Welcome to Meansflow - Where Developers and Students Learn, Share, & Build Careers, where you can ask questions and receive answers from other members of the community.
106 questions
75 answers
36 comments
93 users