8) A king announces a unique reward for Hard Workers. On each day, the reward is multiplied by the day's number. The reward starts at 1 gold coin. Given the number of days n, write a program to calculate the final reward.
2 = 2 * 1
3 = 3 * 2 * 1
4 = 4 * 3 * 2 * 1
nunber = int(input("Enter number of days: "))
reward = 1
days = 1
while days <= number:
reward = reward * days
days += 1
print("Final reward:", reward)
Output:
Enter number of days: 5
Final reward: 120
9) Suppose a password consists of the letters:
A, B. Each letter must be used exactly once. How many different passwords are possible?
1 2
A B
B A
2 --> 2 2!
3 --> 6 3!
4 --> 24 4!
1 2 3
A B C
B A C
B C A
C A B
C B A
A C B
ABCD
- A
AB
ABCD, ABDC
AC
ACBD, ACDB
AD
10) Frog in a Well 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: 1. How many days it takes for the frog to get out of the well. 2. 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.
day total_feet up down total_feet
1 60 2 0.5 total_feet = total_feet -up+down
2 58.5 2 0.5 total_feet = total_feet -up+down
3 57 2 0.5 total_feet = total_feet -up+down
4 55.5 2 0.5 total_feet = total_feet -up+down
total_feet = 60
up = 2
down = 0.5
day = 0
while total_feet>0: #total_feet
total_feet = total_feet - up + down
day = day + 1
print('No. of Days', day)
method1 :
no = 1
while no <=5:
print(no)
no+=1
else:
print(no+10)
OUTPUT:
1 2 3 4 5
16
Method2 :[with break]
no = 1
while no <=5:
print(no) # 1 2 3 4 5
if no == 3:
break #breaks the loop
no+=1
else:
print(no+10) #16
OUTPUT:
1 2 3 4 5
16
11) 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.
PROGRAM:
flowers = 1
temples = 0
while temples < 7:
flowers = flowers * 2
temples = temples + 1
print(flowers)
OUTPUT:
128
flowers = 1
temple=7
for i in range(temple):
flowers = flowers * 2
print("total", flowers)
OUTPUT:
total 128
flowers = 1
temple = 1
while temple <= 7:
flowers = flowers * 2
temple = temple + 1
print("Flowers before first temple =", flowers)
OUTPUT:
Flowers before first temple = 128

Top comments (0)