Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let n=a_k a_k-1 a_k-2...a_1a_0 be an integer and t=a_0 - a_1 + a_2-...+(-1)^k ak

ID: 3670994 • Letter: L

Question

Let n=a_k a_k-1 a_k-2...a_1a_0 be an integer and t=a_0 - a_1 + a_2-...+(-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 II. If n = 54063297, then t = 7-9+2-3+6-0 + 4 - 5 = 2. Because 2 is not divisible by 11.54063297 is not divisible by 11.Write a program that prompts the user to enter a positive integer and then uses this criterion to determine whether the number is divisible by 11. Correctly define variable and use cin Correctly use loop to sum the digits Correctly determine whether the number is divisible by 11 output a message indicating whether the number is divisible by 11.

Explanation / Answer

main.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
   cout << "Enter a positive number => ";
   long long n;
   cin >> n;

   long long t = 0;

   // multiplier while computing t 1 or zero
   int mul = 1;
   while (n > 0) {
       // get the last digit
       long long remain = n % 10;

       // cut off the last digit
       n /= 10;

       // add to t
       t = t + mul * remain;

       // flip mul
       mul *= -1;
   }

   // output the message
   if (t % 11 == 0) cout << "Yes, number is divisible by 11" << endl;
   else cout << "No, number is not divisible by 11" << endl;

   return 0;
}