A Prime number can only be divided by one and itself. For example; 13 can only be divided by 1 and 13. Here’s a C++ code that prints out the all prime numbers between 0 and 100.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | bool isPrime=true; int num = 100; for ( int i = 0; i <= num; i++) { for ( int j = 2; j <= num; j++) { if ( i!=j && i % j == 0 ) { isPrime=false; break; } } if (isPrime) { cout <<"Prime:"<< i << endl; } isPrime=true; } |
A prime number is a positive integer that has exactly two factors, 1 and the number itself. We know 0 is neither a positive nor a negative number. Otherwise, 0 is a neutral number. So, it is not a prime number, and 1 is not a prime number, 1 has only one factor, that’s 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | bool isPrime = true; int num = 100; for ( int i = 0; i <= num; i++) { for ( int j = 2; j <= num; j++) { if ( i!=j && i % j == 0 ) { isPrime=false; break; } } if (isPrime) { System.out.println("Prime: "+i); } isPrime=true; } |