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

Pages: 12
there is something that I can not understand in the following code:

int main()
{

int count = 0;
int x=125;

while(x!=0)
{

count++;

x=x/10;

}


}


if x is 125 so x=x/10 gives 125=12,5 which is something odd and impossible. what is the meaning of x=x/10??? what is x=x/10????? what is its role??? how it works???
any detailed and instructive help or explanation would be appreciated
Do you understand integer division?
int x=125;
Think of this in two stages:
 
x = x/10;

First, calculate the result of the expression on the right hand side of the = symbol. 125/10 gives 12 remainder 5, because the values are integers, the remainder of 5 is discarded, so the result is 12

After that, assign the result to the variable on the left of the =, so x now has the value of 12.

Because this is a loop, the process is repeated next time, giving 1 remainder 2, and the new value of x is 1. The next time around the loop, the result is 0 remainder 1, so x has the value of 0. At that stage, the loop will end, because x!=0 is now false.

So how many times did we go around the loop? Well, count is increased by 1 each time, so it will tell us. And the meaning of count is now the number of decimal digits in the original number 125.
Last edited on
thank you very much

I have questions concerning this piece of code :

int main(){
int num;
bool prime;

cout << "Please enter a positive integer" << endl;
cin >> num;

for(int i = 3; i <= num; 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;
}

how the for loop inside another for loop works??? and more important what is in (int n = 2; n <= i - 1; n++) the (i-1)????? is not i-1 = 3-1=2 so why do not we put 2 instead of ( i-1)??? I am really blocked about this i-1 I did not get its meaning , what is its meaning???????
any help would be appreciated
The inner loop is repeated for each iteration of the outer loop. Only on the first iteration is the (i-1) equal to 2. On the next it is 3, then 4, all the way to num-1.

However, (n <= i - 1) is equivalent to (n < i) in this context. You could rewrite that line as:
for (int n = 2; n < i; n++) {

"However, (n <= i - 1) is equivalent to (n < i) in this context. You could rewrite that line as:
for (int n = 2; n < i; n++) {"


what is the value of i in the context "for (int n = 2; n < i-1; n++) {" ??? and what is the value of (i-1)??? and how it is used in the context of a for loop nested in the body of another for loop like the following:

"for(int i = 3; i <= num; i++){
prime = true;
for(int n = 2; n <= i - 1; n++){" to generate only prime numbers and not a mix between prime and impair numbers ?????????!!!!
strongard63 wrote:
what is the value of i in the context "for (int n = 2; n < i-1; n++) {" ???
That depends on which iteration of the outer loop you are on.
Try to think of the nested loops in this case in terms of "a block of code which does something". For example, look at the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    int num;
    bool prime;

    cout << "Please enter a positive integer" << endl;
    cin >> num;

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



You could think of it in this way:

1
2
3
4
5
6
7
8
9
10
    int num;
    bool prime;

    cout << "Please enter a positive integer" << endl;
    cin >> num;

    for (int i = 3; i <= num; i++)
    {
        // Do something with each value of i
    }

The above code simply repeats the same action for each value of i starting with 3 and ending with num. We don't need to be too concerned with what it is doing.

Then look at the inner loop, this is the "Do something". Here the value of i does not change, it just receives the current value from the outer loop. So when we consider this code, just think of i as being a fixed number.
1
2
3
4
5
6
7
8
9
10
11
12
        prime = true;
        for (int n = 2; n < i; n++)
        {
            if (i % n == 0)
            {
                prime = false;
            }
        }
        if (prime)
        {
            cout << i << " is prime" << endl;
        }
Last edited on
ok , thanks

but why when I replace in the code:

int main(){
int num;
bool prime;

cout << "Please enter a positive integer" << endl;
cin >> num;

for(int i = 3; i <= num; 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 ( i-1) with 2 , it gives me a mix of prime and impair numbers meanwhile when I put the ( i-1) it gives only prime numbers??????????????!!!!

secondly , when I put :

for(int i = 2; i < 10; i++)
{
for (int n = 2; n <=i; n++)
{

it gives me : (2 , 33 , 444 , 5555 , 66666 , 777777 , 8888888 , 99999999) and I understand why as you explained to me and it is simple but what I do not understand is why the numbers are not repeated when the code:

for(int i = 3; i <= num; i++){
prime = true;
for(int n = 2; n <= i - 1; n++){ is compiled???????????
When you replace (i -1) with 2, you must also replace i with 3, on every line where it occurs.

That is, the above code:
1
2
3
4
5
6
7
8
9
10
11
12
13
        prime = true;
        for (int n = 2; n <= i - 1; n++)      // replace i-1 with 2
        {
            if (i % n == 0)                   // replace i with 3 
            {
                prime = false;
            }
        }
        
        if (prime)
        {
            cout << i << " is prime" << endl;  // replace i with 3
        }

result:
1
2
3
4
5
6
7
8
9
10
11
12
13
        prime = true;
        for (int n = 2; n <= 2; n++)          // replace i-1 with 2
        {
            if (3 % n == 0)                   // replace i with 3 
            {
                prime = false;
            }
        }
        
        if (prime)
        {
            cout << 3 << " is prime" << endl;  // replace i with 3
        }


You must be consistent and substitute the same value for i on each line, otherwise you are changing the meaning of the code in an arbitrary way, and the results will change accordingly.

Sorry, i didn't really understand your second example. It would help if you put your code in the proper tags for readability

[code]your code here[/code]

Last edited on
ok there is another something I cannot understand in this code :

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

cout << "Please enter a positive integer" << endl;
cin >> num;

for(int i = 3; i <= num; 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;
}


why it does not print the number 9 and 15 for example although :
9%2 is not equal zero and 15%2 is not equal zero?????????
Because 9%3 == 0 and 15%3 == 0
Lets take Chervil's example:
1
2
3
4
for (int i = 3; i <= num; i++)
  {
    // Do something with each value of i
  }

If we would know beforehand what the num is, then we could "unroll" the loop:
1
2
3
4
5
6
7
8
9
10
int i;
i = 3;
// Do something, i is 3
i = 4;
// Do something, i is 4
i = 5;
// Do something, i is 5
...
i = num;
// Do something, i is num 

Alas, we cannot know the 'num' when we are writing the code, because it is the user who decides the value of 'num' during runtime. Hence we use a loop.
I want to make something clear:

I never studied any programming language at university or at school or at any institution , I have no friends or any sort of people who are experts or students or teachers not even an amateur in computer science or programming , I am alone and all what I get until now was from myself , I study alone and I teach myself , no one gave me any type of support in computer programming this is why I am here in this site because I understood by time that when you study alone you reach a limit which you can never surpass , unless someone pushes you in the right direction or you get involved in an institution


I know how the too loops work:


1
2
3
 for(int i = 3; i <= 10; i++){

        for(int n = 2; n <=i-1 ; n++)



the first loop gives (3,4,5,6,7,8,9,10)
the second loop repeats each number of the first loop n times

for example when I replace i with 3 and (i-1) with 2 , it repeats the number 3 one time . When I replace i by 4 and (4-1) by 3 it repeats the number 4 two times and so on until it reaches the number 10 than it stops and the result is:

(3,44,555,6666,77777,888888,9999999,1010101010101010)

what I do not understand and still do not grasp is how did the two loops give birth to prime numbers??????

LB i know that 9%3 == 0 and 15%3 == 0
but if the first loop gives numbers and the second loop repeats each number of the first , so the question is:
how did you get the (9%3) and the 15%3????????

any further help would be appreciated

what I do not understand and still do not grasp is how did the two loops give birth to prime numbers??????


In simple terms - as I suggested earlier - thinking of the code as "a block of code which does something", it is the inner loop which does the important work. The outer loop is simply feeding numbers, one at a time to the inner loop. What the inner loop does is to test the number, to determine whether or not it is prime.

In order to fully understand what it does, you need to understand not simply how c++ works, but you need to understand how a prime number is defined, and how it behaves.

Let's say we take the number 7, we want to know whether it is prime. So we divide by each integer starting with 2 and less than 7, and see whether there is any remainder:
7 % 2 = 1
7 % 3 = 1
7 % 4 = 3
7 % 5 = 2
7 % 6 = 1
As you can see, there is some remainder every time.
Now try again with the number 15:
15 % 2 = 1
15 % 3 = 0
15 % 4 = 3
15 % 5 = 0
15 % 6 = 3
15 % 7 = 1
15 % 8 = 7
15 % 9 = 6
15 % 10 = 5
15 % 11 = 4
15 % 12 = 3
15 % 13 = 2
15 % 14 = 1

Notice that this time the remainder is zero on two occasions.

For the purposes of testing whether a number is prime, it only has to give a zero remainder once for it to be non-prime, so the testing could have been interrupted after testing 15%3. The remaining tests don't cause harm, but they are a waste of time.

Now if you go back and look at the code above, you will see a variable bool prime which is used in order to keep track of the outcome. It is set to true at the start. If it is still true at the end, the number is prime. If it is false it means there was a remainder of zero at least once, indicating the number has at least one factor, and is therefore not prime.
Last edited on
it is a wonderful explanation and it enlighted me but it remains only one problem if we take this piece of code :

1
2
3
4
5
6
7
8
9
10
11
for(int i = 3; i <= 10; i++){

        for(int n = 2; n <=i-1 ; n++)
        {
       if(i%n!=0)
       {
       cout<<i<<endl;             
       }
               
       }
        }


what it did , according to your explanation is that the first loop gives (3,4,5,6,7,8,9,10)

the second it repeats every number a specific number of times and in the same time it makes the operation (i%n) so that we get your plan :

7 % 2 = 1
7 % 3 = 1
7 % 4 = 3
7 % 5 = 2
7 % 6 = 1


but according to if(i%n!=0) it will show only all the results that are not equal to zero . Thus it shows a repeated 9 and excludes only the (9%3=0) because (9%3=0)

9%2=1
9%3=0
9%4=1
9%5=4
9%6=3
9%7=2
9%8=1

therefore when I compiled the code I got :

5 ,66 , 77777 ,8888,999999,101010101010

so what is the difference between


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

  cout << "Please enter a positive integer" << endl;
  cin >> num;

  for(int i = 3; i <= num; 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;
}

AND


1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(){


     for(int i = 3; i <= 10; i++){

        for(int n = 2; n <=i-1 ; n++)
        {
       if(i%n!=0)
       {
       cout<<i<<endl;             
       }
               
       }
        }


????????????
What is the difference between the two programs?
Well, the first one prints out the prime numbers, and the second one doesn't.

The second program prints the value i each time the remainder of i / n is not zero.
Try changing the second program like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    for (int i = 3; i <= 10; i++)
    {
        for(int n = 2; n <=i-1 ; n++)
        {
            if (i%n!=0)
            {
                // cout<<i<<endl; 
                cout << "i: " << i << "  n: " << n << " i%n: " << i%n << endl;            
            }  
        }
    }
}


Notice for example when i=8, it doesn't print anything when n is 2 or 4, or similarly, when i=6, it doesn't print anything for n = 2 or 3, because the remainder is zero.
you did not get the meaning of my question . when I wrote "What is the difference between the two programs?" I wanted to say why the first code give (3,5,7) meanwhile the other give (5 ,66 , 77777 ,8888,999999,101010101010) although both of codes have the same structure and the same logic and both have the same loops and the same if condition??????
I've no idea how to answer.
It's obvious that the code is not identical.
If you change the code, it does something different.
this is very instructive: when the code changes its result changes , then here is a question :

the first code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    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<<endl;
            } 
       
    }


} 



the result of the first code: 3,5,7


the second code is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    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<<endl;
            } 
            }
       
    }

}


the second result is : 3,55,77777,9

the result is different because of the curly braces in the second code the second if condition is inside the for loop that is nested inside the first meanwhile in the first code the second if condition belongs to the first loop.
When the second if was outside the second for loop , not only the repetition was eliminated but the compiler gives only the prime numbers and not the impair numbers numbers that can have zero as remainder like 9 and 15. In the counterpart , when the second if condition is inserted inside the second for loop , it repeats the numbers like in 7777... and give the impair numbers that have zero as a remainder like 9 and 15.

WHY????

so the question is how do we explain the difference in result in matter of logic and syntax (in this context of course)???? how does the change of curly braces ( in this context ) give a different result???

Pages: 12