cout not working in For Loop

I found this code at tutorialspoint.com and wanted to tweak it a little. It is supposed to print all prime numbers up to 100. I copied and ran it in code::blocks and it works just as it is written.

Original code:
https://www.tutorialspoint.com/cplusplus/cpp_nested_loops.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
 
int main () {
   int i, j;
   
   for(i = 2; i<100; i++) {
      for(j = 2; j <= (i/j); j++)
         if(!(i%j)) break; // if factor found, not prime
         if(j > (i/j)) cout << i << " is prime\n";
   }
	
   return 0;
}



I wanted to try making it print the equation on each iteration of the nested for loop. I added a cout statement before the if statements and put everything in braces, the equations are printing but the if statements aren't printing the prime numbers. I've tried removing the new braces, as it was in the original code, but then nothing is printed. I don't understand what is happening.

This is my new code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main () {
   int i, j;

   for(i = 2; i<100; i++) {
      for(j = 2; j <= (i/j); j++) {
         cout << i << "/" << j << "=" << i/j << endl;
         if(!(i%j)) break; // if factor found, not prime
         if(j > (i/j)) cout << i << " is prime\n";
      }
   }

   return 0;
}
In the original code, line 10 is indented as if guarded by the for statement, but it is not. The indentation is wrong. Some compilers will warn you, if you ask.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main () {
    for(int i = 2; i<100; i++) {
        for(int j = 2; j <= (i/j); j++) {
           std::cout << i << "/" << j << "=" << i/j << std::endl;
           if(!(i%j)) break; // if factor found, not prime
        }
    
        if(j > (i/j)) std::cout << i << " is prime\n";
    }
    return 0;
}
Thanks alot. It's so obvious now that you point it out. I had a feeling it would be something simple.
Try this now:

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
30
31
32
33
34
35
36
37
#include <iostream>

int main () {

    const int UBOUND = 100 ; // prime numbers below this

    // this allows us to eliminate all other even numbers in one fell swoop
    std::cout << "2 is prime\n\n" ;

    // for each odd number from three up to the upper bound
    for( int number = 3 ; number < UBOUND ; number += 2 ) {

         // trial division by odd numbers from 3 up to the square root of the number
         int divisor = 3 ;
         for( ; divisor <= (number/divisor) ; divisor += 2 ) {

            const int remainder = number % divisor ;
            std::cout << number << '/' << divisor << '=' << number/divisor
                      << "  remainder " << remainder << '\n' ;

            if( remainder == 0 ) {

                std::cout << number << " is not prime\n\n" ;
                break ;
            }
         }

         // if divisor is greater than square root, the loop exited without the break
         // (every trial division gave a non-zero remainder; not divisible by any of the divisors)
         if( divisor > (number/divisor) ) {

                std::cout << number << " is prime\n\n" ;
         }

         std::cout << number+1 << " is not prime\n\n" ; // number+1 is an even number greater than two
    }
}
Topic archived. No new replies allowed.