how do i load the array with primes and print
rules -
if the array size is 100 or more
print last 100 element
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int prime(int n)
{
int div = 1;
while(++div <= sqrt(n))
{
if(n % div == 0)
{
return false;
}
}
return n;
}
int main()
{
int max;
int primeindex = 2;
//int storage;
int *primearr;
primearr = new int [max];
cout << "Enter a max number between 2-5000 to find all primes: ";
cin >> max;
if (max > 5000)
{
cout << "Too big, enter a number lower than 5000: ";
cin >> max;
}
//load the array with primes and print
for (int i = 0; i < max; i++)
{
primearr[i] = prime(primeindex);
primeindex++;
if(primearr[i] == 0)
{
cout << "";
}
if (max > 100)
{
for (int j = max-1-100 ; j < max; j++) // to print last 100 elements
{
cout << setw(3) << prime[j];
}
}
else
{
cout << setw(3) << primearr[i];
}
}
return 0;
}
|
Topic archived. No new replies allowed.