Hello to everyone, i recently started to learn c++ with this book "Jumping into C++" from Alex Allain. In this book there is an example of a program to get the prime numbers from 0 to 100 that Im not understanding!
For what I think I understood the second loop of the function "isprime" is going to try to divide from 2 to the number generated by the first loop in function "main" with the modulus operator from the function "isDivisible", a prime number is a number that only as its self and 1 as divisors, but how can the modulus operator "see"
what numbers have only them selfs and 1 as divisors?
If someone could explain the steps of this program to me I would be very grateful.
Heres the code:
#include <iostream>
// note the use of function prototypes
bool isDivisible (int number, int divisor);
bool isPrime (int number);
using namespace std;
int main (){
for ( int i = 0; i < 100; i++ ){
if ( isPrime( i ) ){
cout << i << endl;
}
}
system("pause");
}
bool isPrime (int number) {
for ( int i = 2; i < number; i++){
if ( isDivisible( number, i ) ){
return false;
}
}
return true;
}
bool isDivisible (int number, int divisor){
return number % divisor == 0;
}