DEV Community

Keerthana M
Keerthana M

Posted on

python looping

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 find_vip_number(no):
    div = 2
    while div <= no//2:
        if no % div == 0:
            return 'not VIP'
        div+=1
    else:
        return 'VIP'

no = 2
count = 0
while count < 5:
    result = find_vip_number(no) #Function Calling Statement 
    if result == 'VIP':
        print(no)
        count+=1
    no = no+1
Enter fullscreen mode Exit fullscreen mode

OUTPUT:2 3 5 7 11

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 find_vip_number(no):
    div = 2
    while div <= no//2:
        if no % div == 0:
            return 'not VIP'
        div+=1
    else:
        return 'VIP'

no = 1
while no <= 20:
    result = find_vip_number(no) #Function Calling Statement 
    if result == 'VIP':
        print(no)
    no = no+1
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
1
2
3
5
7
11
13
17
19

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?

chocolate   = 20
wrapper     = 20

while wrapper >= 3:
    wrapper = wrapper - 3
    chocolate = chocolate + 1
    wrapper = wrapper + 1
else:
    print(chocolate)
Enter fullscreen mode Exit fullscreen mode

OUTPUT: 29

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:

  1. For Morning breakfast, he completed 1/3 number of chapathis in the hot box.
  2. For Lunch, He had 1/3 of what was there in the hot box.
  3. For Dinner, again he took and ate 1/3 number of chapathis present in the box.
  4. 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? 5.

```balance = 8
number = 1
while(number <= 3):
total = balance // 2
balance = balance + total
number = number + 1
else:
print(balance)



27
Enter fullscreen mode Exit fullscreen mode

Top comments (0)