#need help with loops task

33 messages · Page 1 of 1 (latest)

tawny sparrow
#

can anyone help me to solve that

floral badgerBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#

@tawny sparrow

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

untold otter
#

what have you tried?

#

are you stuck on anything in particular?

tawny sparrow
# untold otter what have you tried?
    cin >> n;
    bool divisible = true;
    int i;

    for (i = 1; i < n; i++)
    {
        int origin = i;
        int q = origin % 10;
        if (q > 0){
            if (n % q == 0){
                divisible = true;
            }
            origin = origin / 10;
        } else {
            divisible = false;
        }
    }
    
    if (divisible == true){
        cout << i << endl;
    }```
floral badgerBOT
#

@tawny sparrow

It looks like you may have code formatting errors in your message

Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks

Markup

```cpp
int main() {}
```

Result
int main() {}
tawny sparrow
#

i have tried something like that

untold otter
#

you need to print each number that is divisible by its digits right

#

but you have 1 print statement outside your loop

#

so how are you going to print all those values if you only print once?

tawny sparrow
#

it's a big mistake wobter

untold otter
#

also

for (i = 1; i < n; i++)
{
    int origin = i;
    int q = origin % 10;
    if (q > 0){
        if (n % q == 0){
            divisible = true;
        }
        origin = origin / 10;
        // ^ what do you think happens after this line is executed
    } else {
        divisible = false;
    }
}
tawny sparrow
#

if it is 123 as an example q will be 3 after that 2 and 1

untold otter
#

ah but will it?

tawny sparrow
#

i think no because of that divisible = true;

untold otter
#

        origin = origin / 10;
        // 1.
    } else {
        divisible = false;
    }
    // 2. out of the if
}

/// ---
// 3. back to the start of the loop
for (i = 1; i < n; i++)
{
    // 4. back to here
    int origin = i;// 5. make origin for the next i value
#

basicall your problem is that you have 1 loop

#

after you do origin = origin / 10 you're just going straight to the next loop iteration

#

the next value of i

tawny sparrow
#

i undestand it now

#

let me try again

untold otter
#

hint: you need to have whats called a nested loop

#

a loop inside another loop

tawny sparrow
#

i got it

#
    cin >> n;

    for (int i = 1; i <= n; i++) {
        int origin = i;
        bool divisible = true;

        while (origin > 0) {
            int q = origin % 10;
            if (q == 0 || i % q != 0) {
                divisible = false;
                break;
            }
            origin = origin / 10;
        }

        if (divisible) {
            cout << i << " ";
        }
    }

    cout << endl;

    return 0;```
floral badgerBOT
#

@tawny sparrow

It looks like you may have code formatting errors in your message

Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks

Markup

```cpp
int main() {}
```

Result
int main() {}
tawny sparrow
#

finally it works

#

the problem was about using second loop

#

i didn't get that i have to use nested loops

#

thanks for your help and time appreciate that @untold otter

floral badgerBOT
#

@tawny sparrow Has your question been resolved? If so, type !solved :)

tawny sparrow
#

!solved