DEV Community

Saravanan Lakshmanan
Saravanan Lakshmanan

Posted on

Python Looping Programs

The Special Lock Number

A treasure box has a number lock with a number N. The lock designer follows a special rule: A number is considered a Special Number if it can be divided equally into groups in only two ways:
One group containing all the items.
Groups containing one item each.
For example, if the number is 7, the items can be arranged equally only as:
1 group of 7 or 7 groups of 1. But if the number is 8, it can also be arranged as 2 groups of 4, so it is not a Special Number.

n = 100

i = 2

while i <= n//2:
    if n % i == 0:
        print("Not a Prime")
        break
    i+=1    
else:
    print("Prime number")
Enter fullscreen mode Exit fullscreen mode

Output

Prime number

Enter fullscreen mode Exit fullscreen mode

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 findVIPNumbers(n):
    i = 2
    while i <= n//2:
        if n % i == 0:
            return 'Not Prime'
        i+=1    
    else:
        return 'Prime'

no = 2
count = 0

while count < 5:
    result = findVIPNumbers(no)    
    if result == 'Prime':
        print(f'{no} is {result}') 
        count+=1   
    no+=1   
Enter fullscreen mode Exit fullscreen mode

Output

2 is Prime
3 is Prime
5 is Prime
7 is Prime
11 is Prime
Enter fullscreen mode Exit fullscreen mode

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 findVIPNumbers(n):
    i = 2
    while i <= n//2:
        if n % i == 0:
            return 'Not Prime'
        i+=1    
    else:
        return 'Prime'

no = 1
# count = 0

while no <= 20:
    result = findVIPNumbers(no)    
    if result == 'Prime':
        print(f'{no} is {result}') 
        # count+=1   
    no+=1   
Enter fullscreen mode Exit fullscreen mode

Output

1 is Prime
2 is Prime
3 is Prime
5 is Prime
7 is Prime
11 is Prime
13 is Prime
17 is Prime
19 is Prime
Enter fullscreen mode Exit fullscreen mode

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?

amount = 100
oneChocolate = 5
totChocolate = amount // oneChocolate
wrapper = totChocolate

while wrapper >= 3:
    wrapper -= 3
    totChocolate += 1
    wrapper += 1

print(f'Total Chocolates = {totChocolate}')
Enter fullscreen mode Exit fullscreen mode

Output

Total Chocolates = 29

Enter fullscreen mode Exit fullscreen mode

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?

rem = 8

i = 1
while i <= 3:
    rem = rem//2 + rem
    i+=1

print(f'Total = {rem}') 
Enter fullscreen mode Exit fullscreen mode

Output

Total = 27

Enter fullscreen mode Exit fullscreen mode

Top comments (0)