Scenario 1:Divide the tiles equally
Problem:A school is preparing a large square floor for its annual function. The floor has N tiles arranged in a single row. The teacher wants to divide these tiles into equal-sized groups without leaving any tile unused. For example, if there are 12 tiles, the teacher can make:
1 group of 12 tiles
2 groups of 6 tiles
3 groups of 4 tiles
4 groups of 3 tiles
6 groups of 2 tiles
12 groups of 1 tile
Solution:
tiles = 12 groups = 1 while groups <= tiles: if tiles % groups == 0: tiles_per_group = tiles // groups print(groups, "groups of", tiles_per_group, "tiles") groups = groups + 1 Output: 1 group of 12 tiles 2 groups of 6 tiles 3 groups of 4 tiles 4 groups of 3 tiles 6 groups of 2 tiles 12 groups of 1 tile Scenario 2:Secret VIP Numbers
_A security system assigns special numbers to the first few VIP lockers. A number is considered a VIP number if it can be divided exactly by only 1 and itself. The security officer wants the numbers of the first 5 VIP lockers. Write a Python program to display those 5 numbers.
Expected Result: 2, 3, 5, 7, 11_
Solution:
vip_no_count = 0 no = 2 while vip_no_count < 5: divisor = 2 while divisor <= no // 2: if no % divisor == 0: break divisor = divisor + 1 else: print(no) vip_no_count = vip_no_count + 1 no = no + 1 Output: 2,3,5,7,11 Scenario 3:Finding special numbers between 1 and 20.
A teacher is preparing a number Exhibition for students .she writes the numbers 1 to 20 on 20 cards.She wants to select only the special cards.A card is special if its number can be divided exactly by only two numbers. 1 and the number itself.The teacher asks the students :
Solution:
no = 1 while no <= 20: divisor = 2 while divisor <= no // 2: if no % divisor == 0: break divisor = divisor + 1 else: if no > 1: print(no) no = no + 1 Output: 2,3,5,7,11,13,15,17,19 What i learned
- Using while loops to repeat a task.
- Using nested while loops.
- Using counters to control a while loop.
- Using % to check divisibility.
- Using // to calculate whole-number division
- Using break to stop a loop
