+3 votes
29 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 (3.3k points)   | 29 views

Please log in or register to answer this question.

Related questions

+1 vote
0 answers
+2 votes
3 answers
asked May 21, 2020 by athar (3.3k points) | 115 views
+1 vote
1 answer
asked Jun 27, 2020 by fatyma Nauman (555 points) | 71 views
+2 votes
0 answers
asked Jun 12, 2020 by toheed (1.4k points) | 36 views
+1 vote
1 answer
+2 votes
2 answers
asked Jun 4, 2020 by toheed (1.4k points) | 131 views
+1 vote
2 answers
+2 votes
1 answer
+2 votes
1 answer
asked Jun 11, 2020 by noor (1.1k points) | 25 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.
121 questions
92 answers
43 comments
121 users