Explanation:
-
Initialization:
n = 5 -
Condition (while n):
-
In Python, any non-zero integer is treated as True.
-
When n becomes 0, the loop will stop.
-
-
Inside the loop:
print(n, end=" ") → prints the current value of n on the same line.
n //= 2 → integer division by 2 (floor division), updates n.
๐ Step-by-step Execution:
-
First iteration:
n = 5 → prints 5 → update n = 5 // 2 = 2 -
Second iteration:
n = 2 → prints 2 → update n = 2 // 2 = 1 -
Third iteration:
n = 1 → prints 1 → update n = 1 // 2 = 0 -
Fourth iteration:
n = 0 → condition fails → loop ends.
✅ Final Output:
5 2 1


0 Comments:
Post a Comment