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

Recommended Answers

All 12 Replies

You've asked two questions, I'll answer one (Mike's answered the other, see below): This is a common misconception with programming languages like C. It is not an equation. It is an assignment. It means "x is assigned the value of x/10". So it does not mean 125=12.5, it means x=12.5.

Well, 125 / 10 = 12, because this is using integer arithmetic.

So, because the type of x is int (i.e., cannot represent floating-point numbers), then, the meaning of x / 10 is that the value of x is divided by 10, but the remainder is discarded. The remainder is the "rest", i.e., the part that is not divisible by 10. In the case of 125, the remainder is 5, which is discarded. You can also get the remainder alone by using the "modulus" operator (with symbol %), as r = x % 10; which will give you 5 in the case of 125.

This is a common "pitfall" or thing that surprises beginners. This is just the way it works when all the types involved are integers.

thank you very much for you mike and for Assembly guy .
sorry , the format of my message was not good because I do not know how to make a good standarised perfect format.
what was written to me pushes a cloud of darkness from my mind but there are some other things that i am unable to grasp
1)what is the relationship between x=x/10 and the counter(count++)??? in other words , how does x=12.5 , when implemented with the counter (count++) in while loop , give birth to the number of digits in an integer??????????!!!!!! how does this 12.5 give how many digits in the number 125?????

I'll attempt to explain this by showing each iteration of the while loop.
int x = 125

loop 1: count = 1, x = (125/10) which = 12
loop 2: count = 2, x = 12/10 which = 1
loop 3: count = 3, x = 1/10 which = 0
loop ends as x = 0.
So number of digits = 3.
Remember as mike pointed out that this is integer division.

a very wonderful explanation , thank you very much , I have some questions concerning this 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 we do assign to n the number 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

notice= can anyone tell me how to use the format option to write my requests , it is somehow difficult for me , i am not familiar with it

If you have a for-loop like this:

for(int i = 3; i <= num; i++) {
  /* some code */
};

It means that the loop starts by creating a variable called i and initialize it with a value of 3, as if you did int i = 3; on its own. Then, it says that the loop continues as long as i <= num is true, i.e., while i is less-than or equal to num. Finally, it says that between each iteration (i.e., after executing the body of the loop, and before checking the condition), the variable i will be incremented by one, i.e., i++. In other words, the for-loop is equivalent to:

{
  int i = 3;
  while( i <= num ) {
    /* some code */
    ++i;
  };
};

In other words, the logic is this:

for( /* A */ ; /* B */ ; /* C */ ) {
  /* some code */
};

// is equivalent to:

{
  /* A */;
  while( /* B */ ) {
    /* some code */
    /* C */;
  };
};

As for the other for-loop (nested inside the body of the first), it follows the same logic. So, this loop:

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

means that it creates a new variable called n which starts at value 2, then executes the loop while it is less than or equal to i - 1, and increments it by one at each iteration.

Nested loops are no different than other loops, it just means that the complete nested loop is executed at least iteration of the outer loop.

can anyone tell me how to use the format option to write my requests , it is somehow difficult for me , i am not familiar with it

You right your code, then you select it all, and you hit the "Code" button on the editor's header. Another option is to hit the tab key once you selected all your code. A third option is to click the "Code" button before entering the code, which will make a window pop-up in which you can enter your code, and when you are done, the code will be inserted where the cursor was. And finally, another option is to write your code while indenting each line by 4 spaces at least, which does the same thing.

Also, for inline code, like this, you can either select the code and click the "Inline Code" button, or you can start the inline code block with an accent (tick-mark), as so:

`write inline code`

And if you have any doubts on how it will turn out in the final post, you can click the "Spellcheck / Preview" button at any time to see a preview of the results (and some spellchecks).

And finally, if you want people to have an easier time understanding your code, and if you want to understand your code better, you should use proper indentations:

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;
}
   perfect explanation but there is something that I do not understand in the code above:

        the (i-1) in the second loop for(int n = 2; n <= i - 1; n++). What is the value of i??? and consequently what is the value of (i-1)?????

The value of i is whatever its value is in the current iteration of the outer loop. The outer loop is like this:

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

So, at the first iteration, the value of i is 3. At the second iteration, the value is 4. At the third iteration, the value is 5. And so on, until i is no longer less than or equal to num.

And the value of i - 1 is one less than the value of i.

the (i-1) in the second loop for(int n = 2; n <= i - 1; n++). What is the value of i??? and consequently what is the value of (i-1)?????

I gather that your nested loop in its entirety would be:

for (int i = 3; i <= num; i++)
{
    prime = true;

    for (int n = 2; n <= i - 1; n++)
    {
        if(i % n == 0)
            prime = false;
    }
}

The value of 'i' in the inner loop will correspond to what it currently is in the outer loop.
outer loop - for (int i = 3; i <= num; i++), 'i' will initially be 3 then increment by 1 until such time as its greater than num.

edit: yeah, what mike said

ok ,

based on what was written to me , it becomes clear that ,when a for loop is nested inside the body of another loop like in this example:

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

the first loop will give ( 2 , 3,4 , 5,6,7,8,9) and the second will loop each number according to (i-2) times so the result will be :

(2 , 33 , 444 , 5555 , 66666 , 777777 , 8888888 , 99999999)

and if we implemented this piece of code :

if(i % n == 0) {
    cout << i << " is prime" << endl;
}

with the code above we will get:

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

and the result will be:

(2,3,44 ,5 ,666,7,888,99)

how do we explain this chaotic result??? and how to avoid the repetition in (2 , 33 , 444 , 5555 , 66666 , 777777 , 8888888 , 99999999)???? and most importantly why this 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;
}

did not show any repetition of numbers instead it gives prime numbers:

3 ,5 ,7 ,11 ,13 ,......?????????

Well, the result really isn't:

2 3 44 5 666 7 888 99

is it. Write out a table. the table should look something like

i n i%n output
2 2 0 2 is prime
3 2 1
3 3 0 3 is prime
4 2 0 4 is prime
4 3 1
4 4 0 4 is prime
5 2 1
5 3 2
5 4 1
5 5 0 5 is prime
6 2 0 6 is prime
6 3 0 6 is prime
6 4 2
6 5 1
6 6 0 6 is prime
etc

What you posted is a print out of the value of i printed the number of times equal to the number of unique factors in i, including i itself. So it's not chaotic, just a bit obscure on first blush.

The last code snippet says start out by saying until proven otherwise all numbers between 3 and num - 1 are prime. If num % i == 0, then num is not prime, so change the value of prime to false. Note that once prime is set to false in the inner loop it can never to set to true again inside the inner loop. Once the inner loop is completed, if prime remains true throughout the inner loop, then i is prime and print it out. if prime is false by the end of the inner loop do nothing. In either case go through the outer loop again, resetting prime to true before testing that assertion.

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:

 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??????

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

any further help would be appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.