Question:

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
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Read the function.
The function Exam2026 takes a number in the variable given. It sets new to 0 and loops while given is not zero.
Inside the loop it checks if new is odd or even using new % 2.
If new is odd, it adds the last digit of given (given % 10). If new is even, it adds the remainder of given divided by 5 (given % 5).
Then it prints new followed by a hyphen (end='-') and removes the last digit with given //= 10.

Step 2: Pass 1, given = 123456.
new is 0, which is even, so new = 0 + (123456 % 5) = 0 + 1 = 1.
It prints 1-. Then given becomes 12345.

Step 3: Pass 2, given = 12345.
new is 1, which is odd, so new = 1 + (12345 % 10) = 1 + 5 = 6.
It prints 6-. Then given becomes 1234.

Step 4: Pass 3, given = 1234.
new is 6, which is even, so new = 6 + (1234 % 5) = 6 + 4 = 10.
It prints 10-. Then given becomes 123.

Step 5: Pass 4, given = 123.
new is 10, which is even, so new = 10 + (123 % 5) = 10 + 3 = 13.
It prints 13-. Then given becomes 12.

Step 6: Pass 5, given = 12.
new is 13, which is odd, so new = 13 + (12 % 10) = 13 + 2 = 15.
It prints 15-. Then given becomes 1.

Step 7: Pass 6, given = 1.
new is 15, which is odd, so new = 15 + (1 % 10) = 15 + 1 = 16.
It prints 16-. Then given becomes 0, so the loop stops.

Final Answer:
All values are printed on one line because end='-' replaces the newline. A hyphen also follows the last value.
1-6-10-13-15-16-
Was this answer helpful?
0
0