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

Let n-a_ka_k-1ia_k-2...a_ia_0 be an integer and t = a_0 - a_1 + a_2-...+(-1)^k a

ID: 3670991 • Letter: L

Question

Let n-a_ka_k-1ia_k-2...a_ia_0 be an integer and t = a_0 - a_1 + a_2-...+(-1)^k a_k. 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, 1 = 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, 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. Grading scheme: 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;
}