Help! The purpose of this exercise is to list all primes up to 100.
It seems no matter what I do, I either miss at least one prime or I flag some composite numbers as prime. I have been working on this for a couple of weeks now. It is not for a grade so I'm not asking for help with homework. I am using "Programming - Principles And Practice Using C++" by Stroustrup. No answers or hints in book. I don't want to just give up and move on. If anyone can help, thanks a million! (As the code stands now, it lists 45 as prime.)
Thank you. I know. I have been trying to get the inner loop to work with the first few primes and then change the outer loop to get the last few numbers. I've got it working now, but thanks a million!
#include <iostream>
#include <vector>
usingnamespace std;
vector<int> primes;
bool numberisprime(int number, vector<int>primes)
{
for (int i=0; i<primes.size(); i++)
{
if (number%primes[i]==0)
{
returnfalse;
}
}
returntrue;
}
int main()
{
for (int i=2; i<=100; i++) // start at i=2 to stop 1 appearing in list
{
if (numberisprime(i,primes))
{
primes.push_back(i);
cout << i << endl;
}
}
string any; // this line and the next is just to stop
cin >> any; // program closing
return 0;
}