c++ program please A prime number is one which does not have any other factors o
ID: 3732657 • Letter: C
Question
c++ program please
A prime number is one which does not have any other factors other than one and itself. Some prime numbers are: 2, 5, 7, 11..etc. Write a complete c++ program which will accept an integer from the user. The program will then pass this value to the isPrime function which will determine if the number is prime. If the number is prime, then the function will return true, otherwise, it will return false. The program will output if the number is prime or not to both the screen and to an output file. The program will only terminate when the user enters -100
Explanation / Answer
#include <iostream>
using namespace std;
bool isPrime(int n) {
for (int f = 2; f <= n / 2; f++) {
if (n % f == 0) {
return false;
}
}
return true;
}
int main()
{
int n;
cout << "Enter an integer (-100 to quit): " << endl;
cin >> n;
while(n!=-100) {
if(isPrime(n)) {
cout<<"Given number "<<n<<" is a prime number."<<endl;
} else {
cout<<"Given number "<<n<<" is not a prime number."<<endl;
}
cout << "Enter an integer (-100 to quit): " << endl;
cin >> n;
}
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.