#need help with loops task
33 messages · Page 1 of 1 (latest)
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
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
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;
}```
@tawny sparrow
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
```cpp
int main() {}
```
int main() {}
i have tried something like that
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?
it's a big mistake 
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;
}
}
if it is 123 as an example q will be 3 after that 2 and 1
ah but will it?
i think no because of that divisible = true;
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
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;```
@tawny sparrow
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
```cpp
int main() {}
```
int main() {}
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
@tawny sparrow Has your question been resolved? If so, type !solved :)
!solved