DEV Community

vidhya murali
vidhya murali

Posted on

Python Looping Problems

1,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

def findPrime():
    count = 0
    num = 2

    while count < 5:

        isPrime = True

        for i in range(2, num):
            if num % i == 0:
                isPrime = False
                break

        if isPrime:
            print(num)
            count += 1

        num += 1


findPrime()

Enter fullscreen mode Exit fullscreen mode

2,The Special Numbers
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:
“Check all the cards from 1 to 20 and display the numbers that are special.”

def findSpecialNumbers():
    for num in range(1, 21):

        count = 0

        for i in range(1, num + 1):
            if num % i == 0:
                count += 1

        if count == 2:
            print(num)


findSpecialNumbers()
Enter fullscreen mode Exit fullscreen mode

3,You have Rs. 100 /- One chocolate costs Rs. 5. Every time you buy a chocolate, you get one wrapper. You can exchange 3 wrappers for 1 additional chocolate. What is the maximum number of chocolates you can have with Rs. 100?

money = 100
cPrice = 5
wrapper_needed = 3

chocolateCount = money // cPrice
wrapper = chocolateCount

while wrapper >= wrapper_needed:
    wrapper -= wrapper_needed
    chocolateCount += 1
    wrapper += 1

print(chocolateCount)
Enter fullscreen mode Exit fullscreen mode

4,There is one person called Viyan. He is a Software Engineer. He employs a maid for cooking. One day, the maid prepared n number of chapathis and kept in a hot box. Viyan ate those chapathis and the count is as follows:
* For Morning breakfast, he completed 1/3 number of chapathis in the hot box.
* For Lunch, He had 1/3 of what was there in the hot box.
* For Dinner, again he took and ate 1/3 number of chapathis present in the box.

The next day morning, When the maid checked the number of chapathis remaining, it was 8. What was the number of chapathis, She made - in total?

balance = 8

for i in range(3):
    eaten = balance // 2
    balance  += eaten

print("Total chapathis:", balance)
Enter fullscreen mode Exit fullscreen mode

using while loop

balance = 8
count = 0
while count < 3:
    eaten = balance // 2
    balance+= eaten
    count+= 1

print("total", balance)
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

WOW! What a speedy blog post!

Collapse
 
vidhya_murali_5aabe7784bd profile image
vidhya murali

Thank you sir :)