n basic C++, write a function to answer this question. 10. isPrime Function A pr
ID: 3713795 • Letter: N
Question
n basic C++, write a function to answer this question.
10. isPrime Function A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number S is prime because it can only be evenly divided by 1 and S. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6 Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate the function in a complete programExplanation / Answer
#include <iostream>
using namespace std;
bool isPrime(int num) {
for (int f = 2; f <= num / 2; f++) {
if (num % f == 0) {
return false;
}
}
return true;
}
int main() {
int n;
cout<<"Enter the number: "<<endl;
cin >> n;
if(isPrime(n)) {
cout<<"Given number is prime"<<endl;
} else {
cout<<"Given number is not prime"<<endl;
}
}
Output:
Enter the number: 100
Given number is not prime
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.