Divisibility by 11

Interview

Time limit1sMemory limit128 MB

Summary
Apply Dodgson's divisibility-by-11 test to numbers up to 50 digits, printing each intermediate value and the final verdict.
Level

Medium4 of 10

Topics
Math, Implementation, String, Number theory
Solved
No attempts yet

Problem

Write a program that reads a positive integer and, using the algorithm described below, determines whether the integer is divisible by 11. This particular test for divisibility by 11 was given in 1897 by Charles L. Dodgson (Lewis Carroll).

Algorithm:

While the number being tested has more than two digits, form a new number by:

  • deleting the units digit
  • subtracting the deleted digit from the shortened number

The resulting number is divisible by 11 if and only if the original number is divisible by 11.

Input

The first number in the input is the count of the positive integers that follow. Each positive integer has at most 50 digits, and you may assume the integers have no leading zeroes.

Output

For each positive integer, first print the number itself, then print each new value produced by deleting the units digit and subtracting it, one per line (stop once the number has two or fewer digits). Finally, print a sentence stating whether the original number is divisible by 11: use exactly The number X is divisible by 11. when it is divisible and The number X is not divisible by 11. otherwise, where X is the original number. Separate the outputs for different positive integers with a blank line.

Hint

Leading zeroes are not considered part of the number and should not be printed.

Examples2

  1. Example 1

    Input
    1
    12345678901234567900
    
    Expected output
    12345678901234567900
    1234567890123456790
    123456789012345679
    12345678901234558
    1234567890123447
    123456789012337
    12345678901226
    1234567890116
    123456789005
    12345678895
    1234567884
    123456784
    12345674
    1234563
    123453
    12342
    1232
    121
    11
    The number 12345678901234567900 is divisible by 11.
    
  2. Example 2

    Input
    1
    1234567879
    
    Expected output
    1234567879
    123456778
    12345669
    1234557
    123448
    12336
    1227
    115
    6
    The number 1234567879 is not divisible by 11.