c/c++ explanation about x=x/10 in counting the number of digit in an int

Pages: 12
The structure is very different. (Proper indentation could help.) Lets just look at what is inside the outer loop:
1
2
3
4
5
6
7
8
// Program 1:
bool prime = true;
// a loop
// a conditional print

// Program 2:
bool prime = true;
// a loop 

Program 1 prints exactly one value after the loop. The print occurs after all cases have been considered.

Program 2 does something in a loop (possibly multiple times). There is a printout for each case until the first 0-remainder case.
"Program 1 prints exactly one value after the loop. The print occurs after all cases have been considered"


1)HOW???????? please, explain to me in detail

2) and explain to me how does the prime=true inside the first for loop ensures that the number will not be repeated
You have done the loop. Finished. You have looked at every case. You thus know whether any of them was an even division.

Example: 5
Start the loop
-Take 2. Is 5 a multiple of 2? No. Is 5 a prime? We don't know yet.
-Take 3. Is 5 a multiple of 3? No. Is 5 a prime? We don't know yet.
-Take 4. Is 5 a multiple of 4? No. Is 5 a prime? We don't know yet.
Loop has finished.

Now the loop has been completed so we know that we have tested with every number greater than 1 and smaller than 5 (or have been convinced that 5 is not a prime). Now we can ask whether any of them answered "yes" to the "is it a multiple?". The answer is in the variable 'prime'.


Example: 9
Start the loop
-Take 2. Is 9 a multiple of 2? No. Is 9 a prime? We don't know yet.
-Take 3. Is 9 a multiple of 3? Yes. Is 9 a prime? No, so let's make a note of it prime = false;. We have now two options: (a) Continue the rest of the loop; nothing will change the value of 'prime' any more, or (b) Skip the rest of the loop with break;.
...
Loop has finished.

Now the loop has been completed so we know that we have tested with every number greater than 1 and smaller than 9 (or have been convinced that 9 is not a prime). Now we can ask whether any of them answered "yes" to the "is it a multiple?". The answer is in the variable 'prime'.
your reply helped me a lot as it shew me the meaning beyond prime=true

but I am still confused so I will sum up things and make myself more clear and specific

we will take the code piece by piece :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(){
  int num;
  bool prime;


  for(int i = 3; i <= 10; i++){
    prime = true;
    for(int n = 2; n <= i - 1; n++){
      if(i % n == 0){
        prime = false;
      }
    }
    if(prime){
      cout << i << " is prime" << endl;
    }
  }

  return 0;
}


the two loops :

the first loop gives(3,4,5,6,7,8,9,10)
the second loop gives (3, 4,4,5,5,5,6,6,6,6,7,7,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10)

then


the operation(i % n == 0) is made on each number and every time the condition if(i % n == 0) is met the number is eliminated according to


1
2
3
if(i % n == 0){
        prime = false;
      }



what remains after are these numbers:

3,4,5,5,5,6,6,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10,10,10)

why 6,6 and 8,8... and 10 , 10 ....

because:

6%4 is not equal 0 true
6%5 is not equal 0 true

8%7 is not equal 0 true
8%3 is not equal 0 true

10%7 is not equal 0 true
10%6 is not equal 0 true

the condition says that only the numbers that met:

1
2
if(i % n == 0){
        prime = false;


will be false or eliminated like:
10%5=0 false
10%2=0 false

6%3=0 false
6%2=0 false

but

6%4 is not equal 0 so true
10%4 is not equal 0 so true

and so on ....

so the result will be :

3,4,5,5,5,6,6,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10,10,10)


then comes the last part :

1
2
3
 if(prime){
      cout << i << " is prime" << endl;
    }


so if prime is true , it means if prime is not false , it means if i%n is not equal zero , it means this code

1
2
if(i % n == 0){
        prime = false;


so cout << i << " is prime" << endl;

therefore it should give this result:

3,4,5,5,5,6,6,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10,10,10) and not (3,5,7)!!!!!!!!!!???

I need an explanation about all what I wrote

after your reply KESKIVERTO , I understand that prime=true inside the first for loop has a role that I did not get before but now become clear , thanks to you, its role when combined with the for loop is to decide , everytime the loop scans , if the number is true namely if the number % n is not equal zero , namely if the number is not a multiple of another .

but you did not explain HOW it does it , so I would appreciate if you show me with details how ????

and finally if the prime=true inside the for loop would look if number is a multiple or not and following your sentence:

" Now we can ask whether any of them answered "yes" to the "is it a multiple?". The answer is in the variable 'prime'."

everytime , the first loop scans , the numbers (3,4,5,5,5,6,6,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10,10,10) are controled by prime= true the result should be:

(3,5,5,5,7,7,7,7,7) and not (3,5,7)

how what is the problem????
and especially this :

everytime the loop scans , if the number is true namely if the number % n is not equal zero , namely if the number is not a multiple of another it will be kept otherwise it would be eliminated . and the numbers kept will be printed in the screen

you did not explain as I wrote above HOW the mechanism of prime=true inside the first loop is done , so I would appreciate if you show me with details how ????
Forget the outer loop. It is running and has reached the iteration, when i==4. There is nothing else in the world but this body of the loop:
1
2
3
4
5
6
7
8
9
    prime = true;
    for(int n = 2; n <= i - 1; n++){
      if(i % n == 0){
        prime = false;
      }
    }
    if(prime){
      cout << i << " is prime" << endl;
    }

At first i==4 and prime==true.

We enter a loop.
First iteration: n=2. 4%2 == 0, so prime is changed into false.

Second iteration: n=3. 4%3 != 0. Nothing changes prime, and therefore it remains false.

There is no third iteration, because 4 is larger than 3.

Prime is false, so there is no printout.
We enter a loop.
First iteration: n=2. 4%2 == 0, so prime is changed into false.

Second iteration: n=3. 4%3 != 0. Nothing changes prime, and therefore it remains false

I can say the same for

7%3!=0

10%4!=0 and so on ............

sorry bro , but I did not get your meaning except this sentence "Forget the outer loop" it is a very instructive sentence

but the rest I did not get it and you did not answer some of my questions

explain to me your meaning bro and ,if possible , some of my questions
strongard63 wrote:
Second iteration: n=3. 4%3 != 0. Nothing changes prime, and therefore it remains false

I can say the same for

7%3!=0

10%4!=0 and so on ............
What about 4%2 or 10%2 or 10%5?
I can say the same for

7%3!=0

Can you?

Now you are considering the case, when i==7.
We start by setting prime true.
Do the loop:
n=2. 7%2 != 0, so nothing changes prime, and therefore it remains true.
n=3. 7%3 != 0, so nothing changes prime, and therefore it remains true. [A]
n=4. 7%4 != 0, so nothing changes prime, and therefore it remains true.
n=5. 7%5 != 0, so nothing changes prime, and therefore it remains true.
n=6. 7%6 != 0, so nothing changes prime, and therefore it remains true.
Loop is complete and the prime is still true.

Now we can conclude that 7 is a prime.

[A] The inequality means that the value of prime does not change. Now it is up to you to decide whether prime not changing from true is the same as prime not changing from false.

Lets do the i==10 too.

We start by setting prime true.
Do the loop:
n=2. 10%2 == 0, so prime is changed into false.
n=3. 10%3 != 0, so nothing changes prime, and therefore it remains false.
n=4. 10%4 != 0, so nothing changes prime, and therefore it remains false.
n=5. 10%5 == 0, prime is again set to false, but since it already was false, it does not change.
n=6. 10%6 != 0, so nothing changes prime, and therefore it remains false.
n=7. 10%7 != 0, so nothing changes prime, and therefore it remains false.
n=8. 10%8 != 0, so nothing changes prime, and therefore it remains false.
n=9. 10%9 != 0, so nothing changes prime, and therefore it remains false.
Loop is complete.

At some point during the loop the prime has been set false, so 10 is not a prime number.

You don't see any pattern in these examples, do you?


I realize that there is certain semantic hiccup, when false means that the answer to "Is n a multiple of some smaller numbers?" is "Yes".


How about a variation of the code:
1
2
3
4
5
6
7
8
9
10
11
    prime = true;
    for(int n = 2; n < i; ++n){
      if(i % n == 0){
        prime = false;
        break;
      }
    }

    if(prime){
      cout << i << " is prime" << endl;
    }
How about a variation of the code:




1
2
3
4
5
6
7
8
9
10
11
 prime = true;
    for(int n = 2; n < i; ++n){
      if(i % n == 0){
        prime = false;
        break;
      }
    }

    if(prime){
      cout << i << " is prime" << endl;
    }


whenever the i%n is equal to 0 the number will be set to false and the loop will stop and then it will look for the next number and so on

ok , my doubts are gone

thank you for your efforts keskiverto



Topic archived. No new replies allowed.
Pages: 12