Answer:
// here is code in C++.
#include <bits/stdc++.h>
using namespace std;
// function to check prime
bool isPrime(int n)
{
// if input is less than 1
if(n<1)
return false;
// if input is 1,2 or 3
else if (n == 1||n ==2 ||n==3)
{
return true;
}
// if input is greater than 3
else
{
for(int i=2; i<n; i++)
{
if(n%i==0)
return false;
}
return true;
}
}
// driver function
int main()
{
// variable
int num;
cout << "Enter a number: ";
// read the input number
cin >> num;
// call function to check prime
if (isPrime(num)==true)
// if prime
cout << num << " is prime.";
else
// if not prime
cout << num << " is not prime.";
return 0;
}
Step-by-step explanation:
Read a number from user and assign it to "num".Then call the function isPrime() with "num" as parameter.In this function, if the input is less than 1 then it will return "false".If input is 1 or 2 or 3 then it will return "true".For an input greater than 3, if the number is divisible by only 1 and itself then function will return "true" otherwise it will return "false".
Output:
Enter a number: 17
17 is prime.