As the title says, i'm trying to write a program that will print out the all prime numbers below 100..
What i've been trying to write in code is:
Check if any of the numbers less than a is divisible with a. if yes than it's not a prime, if none of the numbers less than a is divisible by a it's a prime
#include <iostream>
usingnamespace std;
int Search = 1, a = 2;
int main()
{
while(a < 100)
{
for (a; Search < a; Search++)
{
if(a % Search == 0)
{
Search = 1;
break;
// not prime
}
}
if(a % Search != 0) // if a % Search did not = 0 during the whole for loop than it's a prime
{
int prime = a;
Search = 1;
cout << prime << endl;
}
a++;
}
system("pause");
return 0;
}
Well, it's a number that is only divisible by 1 and itself.
that's why i'm first having an if statement that checks the for loop if "a" is divisible by any value that "search" obtains if it is not then it goes on then it breaks out of the foor loop and adds a value to "prime"
int Search = 2, a = 2;
int main()
{
while(a < 100)
{
for (a; Search < a; Search++)
{
if(a % Search == 0)
{
Search = 2;
break;
// not prime
}
}
if(a % Search != 0) // if search did not = 0 during the whole for loop than it's a prime
{
int prime = a;
Search = 2;
cout << prime << endl;
}
a++;
}
system("pause");
return 0;
}
now, imagine Search HAD to be 1, you can't change what value it starts as, find some other way to make sure it doesn't check "a % Search == 0" when Search == 1
#include <iostream>
usingnamespace std;
int Search = 1, a = 2;
int main()
{
while (a < 100)
{
for (; Search < a; Search++)
{
if (Search != 1 && a % Search == 0)
{
Search = 1;
break;
}
}
cout << a << " % " << Search << " = " << a % Search << endl;
if (a % Search != 0)
{
int prime = a;
Search = 1;
cout << prime << endl;
}
a++;
}
return 0;
}
what if you moved Search != 1 to the other if statement? what if you had them in both if statements?
experiment a little bit, see what you come up with, what if you assumed it was a prime unless you had proof it wasn't? or vice-versa? how would you store something like that in a variable, what type would you use?