Shouldn't this calculate all prime numbers to 100?

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:


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
#include <iostream>

using namespace 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!
Last edited on
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
#include <iostream>

using namespace 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;
}


hope it works
I will make some changes to above program to make it fast
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main ()
{
    for (int i = 2; i <= 100; ++i)
    {
		bool isprime=true;
		for (int divisor = 2; divisor < i/2; ++divisor )
                   if (!(i%divisor))
				   {
					   isprime=false;
					   break;
				   }
	    if(isprime)
			cout<<i<<endl;
    }

cout<<"\nAll prime numbers are accounted for!\n";   
cin.get();
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!
bools means true or false.You can also use int flag=0; and then change flag inside loop at place isprime was changed and check condition later.

!(i%divisor) means "NOT(i%divisor)" or as i%divisor==0
"!" converts true to false and vice versa.(0 means false as you might know)

for checking prime you only need to run loop to i/2 and thats related to maths.

Get idea of loops and condition thoroughly before understanding this.

What you were doing was running loop was nothing.It had many problems.You used second loop to run but do nothing good.

if you know functions then use seperate function for checking prime(if not then leave it) like
1
2
3
4
5
6
7
8
9
10
11
int checkprime()
{
                int isprime=1;
		for (int divisor = 2; divisor < i/2; ++divisor )
                   if ((i%divisor)==0)      //or !(i%divisor)
				   {
					   isprime=0;
					   break;
				   }
            return isprime;       //0 if false else 1 for true
}
or with bool
I still don't understand... Sorry for being slow! but thanks a lot for helping me!
Last edited on
Why did mines not work?
What you were doing
1
2
3
4
5
6
7
8
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 
Last edited on
If
x % y == 0 && x / y == 1
then
x == y.

You wrote;
if (divisor == i && i%divisor == 0 && i/divisor == 1)

which is equivalent to:
if (divisor == i)

And I'm sure your algorithm isn't attempting to perform that check.
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!

THANK YOU!!!
Last edited on
bool is described as a basic data type here:
http://www.cplusplus.com/doc/tutorial/variables/

The boolean operators and expressions are explained here:
http://www.cplusplus.com/doc/tutorial/operators/
(even tho I still don't understand, I feel a bit ashamed considering I'm going to be an 11th grader this fall)


I first heard about c++ was my 11th grade and I just completed my 12th.
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 am not a professional.I just make program out of my mind.Game building is something that will require more knowledge(proper games) and time.

I have made 3-4 big programs just using beginners knowledge till file handling.As I said I am not professional.
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.
Topic archived. No new replies allowed.