finding a prime number

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>
using namespace 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);


}
Last edited on
This is a basic algorythm to find a prime number:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>
using namespace 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
		    return false;
		}
	}

	return true;
}

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";

}
Topic archived. No new replies allowed.