hello everyone, this is my first post. I have been teaching myself c++ for the last few weeks and have gotten more familiar with some of it.
i thought to myself it would be interesting to try writing up some code that goes through numbers to see if they are prime or not, and if the number is prime, it outputs it on the screen.
the way i coded it was like this... and i feel like it is VERY close to being right, but whenever i run it, it just outputs EVERY number, not just the prime numbers...
can anyone let me know what i need to change in this code to make sure that it ONLY outputs the primenumbers, and so that it continues the loop afterwards so that it keeps looking for prime numbers up to the max value of an unsigned integer...
here is the code
#include <iostream>
#include <string>
using namespace std;
int main()
{
unsigned int isitprime = 3;
unsigned int basenumber = 2;
unsigned int placekeeper = 2;
float askit = (float)isitprime/(float)basenumber;
do{
if(askit==placekeeper)
{
placekeeper = 2;
basenumber = 2;
++isitprime;
}
if(askit!=placekeeper)
{
do
{
++placekeeper;
}
while(placekeeper<isitprime);
if(placekeeper<=isitprime)
{
placekeeper = 2;
++basenumber;
}
if(basenumber==isitprime)
{
cout << isitprime << ", ";
++isitprime;
basenumber = 2;
placekeeper = 2;
}
}
}
while (isitprime<=222222222);
}
now that i look at the code, i'm thinking i could probably take out the #include <string> part.... but thats irrelevant. what is wrong with this code? i haven't been able to figure it out yet.