English

Write def 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) - Computer Science (Python)

Advertisements
Advertisements

Question

Write def 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)
Long Answer
Advertisements

Solution

Step-by-Step Execution:
  • Iteration 1 (given=123456): new (0) is even, new += 6 % 5 (1). Print 1-. given becomes 12345.
  • Iteration 2 (given=12345): new (1) is odd, new += 5 % 10 (5). Print 6-. given becomes 1234.
  • Iteration 3 (given=1234): new (6) is even, new += 4 % 5 (4). Print 10-. given becomes 123.
  • Iteration 4 (given=123): new (10) is even, new += 3 % 5 (3). Print 13-. given becomes 12.
  • Iteration 5 (given=12): new (13) is odd, new += 2 % 10 (2). Print 15-. given becomes 1.
  • Iteration 6 (given=1): new (15) is odd, new += 1 % 10 (1). Print 16-. given becomes 0.

Final output: 1-6-10-13-15-16- 

shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Set 4

APPEARS IN

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×