Listing prime numbers?

For one of my school projects I have to make a function that can basically list the prime numbers of the total set of numbers you insert.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<cmath>
#include<string>    //I know I don't need all these libraries.
#include<time.h>
#include<cstdlib>
#include<ctype.h>
using namespace std;

int listPrimesNumbers(int a)
{
 // Don't know how to do this function
}	
 int main()
{
	
	listPrimeNumbers(20); // any number set within here

	  return 0;
}


How would I figure out if the numbers within 1 to 20 for example are prime numbers? Any help is appreciated. Thanks.
What is the range of numbers you HAVE to do for the project, is 1-20 all? If so that shouldn't be nearly as bad as letting a user input a range...

take a look at the 5th post down, by tej

http://www.cplusplus.com/forum/beginner/72609/
Last edited on
basically the range would have to be from 1 to whatever number is inserted in listPrimeNumbers(x) where x would be the number if that makes any sense.
you for loop

1
2
3
4
5
6
7
int dividers = 2;  //  your number will have at least 2 dividers 

for(int i = 2; i < sqrt(x); i++)  
{
         if(x % i == 0)
                dividers++;
}


then you have to check how many dividers current number has - if 2 it's a prime number
Last edited on
Topic archived. No new replies allowed.