IsPrime | School Method
Input: n = 11
Output: true
Input: n = 15
Output: false
Input: n = 1
Output: false// A school method based C++ program to check if a
// number is prime
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n)
{
// Corner case
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;}
// 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;
}
Last updated