#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 12;
int i, res;
string digits = to_string(n);
for(i = 0; i < digits.length(); i++){
res = 0;
res = n % digits[i];
if (res != 0){
res = false;
break;
}
if (res == 0){
res = true;
}
}
The digits[0] is 1, therefore: 12 % 1 = 0 = res. However, when I run the program, res = 12, so the following if conditions do not work correctly. What's wrong?
Thanks
ifconditions are not in the loop, so they're just usingresfrom the last digit.digits[0]is 49, not 1.charas a number, you get the character code.