Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3) Write a program that indicates if a number input is a prime number or not. Th

ID: 3870491 • Letter: 3

Question

3) Write a program that indicates if a number input is a prime number or not. The program should repeat this until the user enters 0 to exit. For example, the input of 17 should print "prime", since 17 has no divisors other than 17 and 1 (hence 17 is a prime number). Input of 21 should print "not a prime" (3 and 7 are both divisors of 21, hence 21 is not a prime number) Run the program using your own Red ID number to determine if it is a prime number The program should use the exhaustive search approach to finding a divisor for n. It should start by checking to see if 2 is a divisor, and then checking successive integers until it finds a divisor (in which case it returns 0) or it is certain that there are no divisors (in which case it returns 1). Give some thought to the question “ what is the highest number that needs to be checked?" You might think initially that it is n/2, but it is smaller than that.

Explanation / 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 (0 to quit): ";

cin >> n;

while(n != 0) {

if(isPrime(n)) {

cout<<"prime"<<endl;

} else {

cout<<"not a prime"<<endl;

}

cout<<"Enter the number (0 to quit): ";

cin >> n;

}

return 0;

}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote