Answers for "c++ check prime"

C++
1

c++ check prime

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int prime;
    cin >> prime; // input should be greater then 0
    bool is_prime = true;

    for (int i = 2; i * i <= prime; i++)
    {
        if (prime % i == 0)
        {
            is_prime = false;
            break;
        }
    }

    if (is_prime)
    {
        cout << "Prime number" << endl;
    }
    else
    {
        cout << "Not Prime number" << endl;
    }
    return 0;
}
Posted by: Guest on July-28-2021

Browse Popular Code Answers by Language