I've been learning c++ for about a month and I'm still stuck on the basics. I know I could just search up how to calculate prime numbers on google or on this forum, but I believe it is better to understand rather than reproduce so,
heres my code:
#include <iostream>
usingnamespace std;
int main ()
{
for (int i = 2; i < 100; ++i)
{
int divisor = 1;
int j = i;
++j;
for (; divisor < j; ++divisor )
{
if (divisor == i && i%divisor == 0 && i/divisor == 1)
{
cout<<i<<endl;
}
}
}
cout<<"\nAll prime numbers are accounted for!\n";
return 0;
}
To my logic, this should execute prime numbers, but it doesn't. Can somebody tell how I should fix this code or improve my logic of c++? Any help would be appreciated!
#include <iostream>
usingnamespace std;
int main ()
{
for (int i = 2; i < 100; ++i)
{
bool isPrime=true;
for (int divisor = 2; divisor < i and isPrime; ++divisor )
{
if (!(i%divisor))
{
isPrime=false;
break;
}
}
if(isPrime) {
cout<<i<<endl;
}
}
cout<<"\nAll prime numbers are accounted for!\n";
return 0;
}
I don't get it... Why did mines not work? And why does this one work? And Why do all these little addons you made to the code make it execute correctly? And it it's not a problem could somebody direct me to a THOROUGH tutorial on boolean expressions because i don't understand what if (!(i%divisor )) means.
Also, thanks akshit and script coder for your replies!
for (int i = 2; i < 100; ++i){
int divisor = 1;
int j = i;
++j;
for (; divisor < j; ++divisor )
if (divisor == i && i%divisor == 0 && i/divisor == 1)
cout<<i<<endl;
}
1 2 3 4 5 6
for (int i = 2; i < 100; ++i){
int divisor = 1;
for (; divisor < i+1; ++divisor ) //j=i+1, so this is equivalent
if (divisor == i) //if divisor is i, then it will divide, giving 1
cout<<i<<endl;
}
1 2
for (int i = 2; i < 100; ++i)
cout<<i<<endl; //your second loop reduces to this
As for booleon expressions, in C++ a non-bool value used in booleon arithmetic defaults to false if 0 or true if non-zero.
For example, a C-string ends with a null-character which has an integer value of 0, so for (int i = 0; str[i]; ++i);
cycles through a C-string until the null-character is found because every character other than the null character is 'true', AKA keep looping, and the null character having a value of 0 is 'false'.
if (divisor == i && i%divisor == 0 && i/divisor == 1) // FAILS becuz i willl always eventually be divided by itself
Thanks ne555 for helping me understand what was wrong with my code with simplicity.
Sometimes I feel like an idiot when sumthing I expect to work doesn't.
I would also like to thank Veltas and akshit for helping me understand boolean values
(even tho I still don't understand, I feel a bit ashamed considering I'm going to be an 11th grader this fall)
Just an overall thanks to everyone who replied to my post!
So, how long did it take you to start making applications or games and stuff in c++, cuz I've been going at it for about a month and I'm still doing the basics.
I'd say the use of APIs and libraries is something reasonably basic. Doing a good job and using them correctly is a real skill, but you need to make crap programs to make good ones... good luck with your C++ endeavours.