hello, so part of my assignment is to call for a prime number using a bool function. Here's what i have so far. I know what the problem is with my function, but am having a difficult time wording it the way i am thinking it. obviously, anything divided by 2 is going to have a remainder (prime%i), i feel like i am close, but i can't quite get the cigar. one thing i have found is called the "sieve of Eratosthenes," which is an algorithm to finding a prime number, but im just need some helping translating it to code.
#include <iostream>
#include <cstdlib>
usingnamespace std;
bool isPrime(int number) {
//finding a prime number
int prime = 1;
for (int i = 2; i <= number; i++){
if (prime%i != 0){
cout << prime << endl;
prime++;
}
}
return 0;
}
int main() {
int input;
cout << "Please enter a positive number: ";
cin >> input;
printf; isPrime(input);
}
#include <iostream>
#include <cstdlib>
usingnamespace std;
bool isPrime(int number) {
//finding a prime number
for (int i = 2; i < number; i++){
if (number%i == 0){ // Note: no remainder -> not a prime number
returnfalse;
}
}
returntrue;
}
int main() {
int input;
cout << "Please enter a positive number: ";
cin >> input;
if(isPrime(input))
cout << endl << "prime number";
else
cout << endl << "Not a prime number";
}