# Answer to Question #58184 in C++ for M

Question #58184

Let n = ak ak-1 ak-2. . .a1 a0 be an integer and t = a0 - a1 + a2 - …+ (-1)k ak. It is known that n is divisible by 11 if and only if t is divisible by 11. For example, suppose that n = 8784204. Then, t = 4 - 0 + 2 - 4 + 8 - 7 + 8 = 11. Because 11 is divisible by 11, it follows that 8784204 is divisible by 11. If n =54063297, then t = 7 - 9 + 2 - 3 + 6 - 0 + 4 - 5 = 2. Because 2 is not divisible by 11 then n is not divisible by 11.

Expert's answer

#include <iostream>

#include <string>

using namespace std;

int main() {

long num;

cin >> num;

int sum = 0, coef = 1;

while (num>0) {

sum = sum + (num % 10) * coef;

coef *= -1;

num /= 10;

}

if (sum % 11 == 0)

cout << "Number is divisible by 11.";

else cout << "Number is not divisible by 11.";

return 0;

}

#include <string>

using namespace std;

int main() {

long num;

cin >> num;

int sum = 0, coef = 1;

while (num>0) {

sum = sum + (num % 10) * coef;

coef *= -1;

num /= 10;

}

if (sum % 11 == 0)

cout << "Number is divisible by 11.";

else cout << "Number is not divisible by 11.";

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment