Question:hard

Write the output of the following code:

def Exam2026(given):
    new = 0
    while given:
        if new % 2:
            new += given % 10
        else:
            new += given % 5
        print(new, end='-')
        given //= 10
Exam2026(123456)

Show Hint

Trace the loop pass by pass. Use the old value of new to choose between adding given % 5 and given % 10.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Know what the print does.
The print uses end='-', so no new line is made. Every value of new is followed by a hyphen on the same line.
The loop runs once for each digit of 123456, so there are 6 passes and 6 printed values.

Step 2: Note the digits and the rule.
The digits are taken from the right: 6, 5, 4, 3, 2, 1.
The number left in given before each pass is: 123456, 12345, 1234, 123, 12, 1.
The rule is: if the old new is even, add given % 5. If the old new is odd, add given % 10.

Step 3: Fill the trace table.
Pass 1: old new 0 (even), given % 5 = 1, new = 1.
Pass 2: old new 1 (odd), given % 10 = 5, new = 6.
Pass 3: old new 6 (even), given % 5 = 4, new = 10.
Pass 4: old new 10 (even), given % 5 = 3, new = 13.
Pass 5: old new 13 (odd), given % 10 = 2, new = 15.
Pass 6: old new 15 (odd), given % 10 = 1, new = 16.

Step 4: Common mistakes to avoid.
The test uses the old value of new, not the new value. So pass 2 uses the odd rule, because new is 1 after pass 1.
Also, 123456 % 5 is 1, because 123455 is a multiple of 5.
The last print is 16- and not 16, because end='-' is applied to every print.

Final Answer:
The printed output is:
1-6-10-13-15-16-
Was this answer helpful?
0