1. Display Odd Numbers First and Even Numbers Next
Problem: In a school sports event, all students wearing ODD jersey numbers enter the ground first. After they finish, students with EVEN jersey numbers enter. Display the order up to 10.
Step 1: Display Odd Numbers
count = 1
while count <= 10:
if count % 2 != 0:
print(count)
count = count + 1
Output:
1
3
5
7
9
Step 2: Display Even Numbers
count = 1
while count <= 10:
if count % 2 == 0:
print(count)
count = count + 1
Output:
2
4
6
8
10
Remember:
- number % 2 != 0 checks whether a number is odd.
- number % 2 == 0 checks whether a number is even.
2. The Father's Savings Plan
Problem:A daughter is going on a 5-day vacation. She asks her father to give her Rs. 5 every day, so she can save the money.
Her father suggests a different plan. Instead of giving her Rs. 5 daily, he says:
Day 1: Rs. 1
Day 2: Rs. 2
Day 3: Rs. 3
Day 4: Rs. 4
Day 5: Rs. 5
The daughter agrees to her father's proposal.
Write a program to calculate:
A) The total amount the daughter would receive under her original plan.
B) The total amount she would receive under her father's plan.
A) Original Plan: ₹5 Every Day
box = 0
day = 1
while day <= 5:
box = box + 5
day = day + 1
print("Original Plan:", box)
Output:
Original Plan: 25
B) Father's Plan: Increasing Amount Every Day
box = 0
day = 1
while day <= 5:
box = box + day
day = day + 1
print("Father's Plan:", box)
Output:
Father's Plan: 15
The daughter receives ₹25 under her original plan and ₹15 under her father's plan.
3. Addition of the First 10 Numbers
Problem: Calculate the sum of numbers from 1 to 10.
total = 0
n = 1
while n <= 10:
total = total + n
n = n + 1
print(total)
Output:
55
Here:
- total = 0 initializes the sum.
- total = total + n adds the current number.
- n = n + 1 moves to the next number.
4. Multiplication of the First 5 Numbers
Problem: Calculate the product of numbers from 1 to 5.
total = 1
n = 1
while n <= 5:
total = total * n
n = n + 1
print(total)
Output:
120
Calculation:
1 × 2 × 3 × 4 × 5 = 120
Important: For addition, we initialize total with 0. For multiplication, we initialize it with 1.
5. Frog in a Well
Problem:A frog accidentally falls into a 60-foot-deep well.
Every day:
- During the day, the frog climbs 2 feet.
- During the night, it slips down 0.5 feet.
This pattern continues every day until the frog reaches the top of the well and escapes.
Write a program to determine:
- How many days it takes for the frog to get out of the well.
- On which day the frog escapes.
Note: Once the frog reaches or crosses the top of the well during the daytime, it escapes immediately and does not slip back that night.
Solution
well_depth = 60
day_climb = 2
night_climb = 0.5
days = 0
climb = 0
while climb < well_depth:
days = days + 1
climb = climb + day_climb
if climb >= well_depth:
break
climb = climb - night_climb
print("Days taken:", days)
Output:
Days taken: 40
Why do we use break?
The frog may reach the top during the daytime. So, we check its position immediately after climbing.
If it reaches or crosses 60 feet, break stops the loop. Otherwise, the frog slips down 0.5 feet at night.
6.The Saint's Flower Basket
Problem:The Saint's Flower Basket
A saint goes for a morning walk every day. During his walk, he plucks flowers and keeps them in a basket.
On his way back home, he visits 7 temples. At each temple, he offers half of the flowers currently in his basket.
After visiting all 7 temples, he reaches home with exactly one flower remaining in his basket.
Write a program to determine how many flowers the saint had in his basket before he visited the first temple.
Hint: Think carefully about whether it is easier to solve the problem from the beginning or by working backwards from the final flower
Solution: Work Backwards
If one flower remains after the seventh temple, then before the seventh temple he had 2 flowers.
Before the sixth temple, he had 4 flowers.
We keep doubling the number to work backwards.
num_temples = 7
count = 1
while num_temples > 0:
count = count * 2
num_temples = num_temples - 1
print("Initial flowers:", count)
Output:
Initial flowers: 128
The saint initially had 128 flowers.
Top comments (0)