A prime number is an integer greater or equal to 2 that is only divisible by 1 a
ID: 3723937 • Letter: A
Question
A prime number is an integer greater or equal to 2 that is only divisible by 1 and by itself. The first few primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 … N is a prime if and only if it is not divisible evenly by any of the numbers from 2 to N1. Let’s implement this decision as a function. In the same program numbers.cpp, add a function bool isPrime(int n); The function should return true if n is a prime, otherwise return false. Change the main function to test your new code
Explanation / Answer
#include <iostream>
using namespace std;
bool isPrime(int n);
int main(){
int number;
cout<<"Enter any number: ";
cin>>number;
if (isPrime(number)) //return true if n is a prime, otherwise return false.
cout<<"prime number";
else
cout<<"Not a prime number";
return 0;
}
bool isPrime(int n)
{
bool flag=true;
for(int i=2;i<n;i++) {
if(n%i == 0) {
return false;
}
}
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.