DEV Community

Cover image for Python Practice problems-2
Vigneshwaran V
Vigneshwaran V

Posted on

Python Practice problems-2

1. Skip the Number 7 Using continue

Question

Write a Python program to check whether a given number is divisible by numbers from 2 to 9, but skip the number 7.

Code

num = int(input('Enter Number: '))
i=2
while (i<=9):
    if i==7:
        i=i+1
        continue
    if num%i==0:
        print(num," is divided by ",i)
    else:
        print(num," is not divided by ",i)

    i=i+1
Enter fullscreen mode Exit fullscreen mode

Output

Enter Number: 42
42  is divided by  2
42  is divided by  3
42  is not divided by  4
42  is not divided by  5
42  is divided by  6
42  is not divided by  8
42  is not divided by  9
Enter fullscreen mode Exit fullscreen mode

2. Find the Lowest and Highest Prime Numbers

Question

Write a Python program to find the lowest prime number starting from 11 and the highest prime number less than or equal to 99.

Code

def isPrime(num):
    if(num<2):
        return False
    i=2
    while i<=num//2:
        if(num%i==0):
            return False
        i=i+1

    return True

num=99
copy=num
i=11
while i<=copy:
    if(isPrime(num)):
        if(isPrime(i)):
            print(f"lowest prime: {i}\nhighest prime: {num}")
            break
        else:
            i=i+1
    else:
        num = num - 1       
Enter fullscreen mode Exit fullscreen mode

Output

lowest prime: 11
highest prime: 97
Enter fullscreen mode Exit fullscreen mode

3. Find When All Four Phones Ring Together

Question

Four phones ring every 15, 20, 25, and 30 minutes respectively. Write a Python program to find after how many minutes all four phones ring together.

Assume the starting time is 5 O'clock.

Code

phone1=15
phone2=20
phone3=25
phone4=30
num=1
time=5
while True:
    if num%60==0:
        time=time+1
    if(num%phone1==0 and num%phone2==0 and num%phone3==0 and num%phone4==0):
        print(f"After {num} minutes")
        break
    num=num+1

print(f"{time} O'clock")
Enter fullscreen mode Exit fullscreen mode

Output

After 300 minutes
10 O'clock
Enter fullscreen mode Exit fullscreen mode

4. Check Whether Two Numbers Are Amicable Numbers

Question

Write a Python program to check whether 1184 and 1210 are amicable numbers.

Code

num1=1184
num2=1210

def findAmicable(num):
    fact=0
    i=1
    while i<=num//2:
        if(num%i==0):
            fact=fact+i
        i=i+1
    return fact

if(findAmicable(num1) == num2 and findAmicable(num2) == num1):
    print(f"{num1} and {num2} are amicable numbers")
Enter fullscreen mode Exit fullscreen mode

Output

1184 and 1210 are amicable numbers
Enter fullscreen mode Exit fullscreen mode

Top comments (0)