DEV Community

Bhagyashree
Bhagyashree

Posted on

Practice Problems in Python

Task - 1 : The number of prime numbers between 11 and 60

1. Concept

A prime number is a number greater than 1 that has exactly two factors: 1 and itself.

Examples: 2, 3, 5, 7, 11, 13, ...

To find the number of prime numbers between 11 and 60, check each number in the range and count those that are prime.
Prime numbers between 11 and 60 are:

11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59
Count=13

2. Logic

  1. Start from 11 and go up to 60 using a while loop.
  2. For each number, check whether it is divisible by any number from 2 to √n using another while loop.
  3. If it is not divisible by any number, it is prime.
  4. Increase the count of prime numbers.
  5. Display the final count.

3. Algorithm

Step 1: Start

Step 2: Initialize n = 11 and count = 0

Step 3: Repeat while n <= 60

  • Set prime = True
  • Set i = 2
  • Repeat while i <= √n

    • If n % i == 0
    • Set prime = False
    • Exit inner loop
    • Increment i
  • If prime == True

    • Increment count
  • Increment n

Step 4: Display count

Step 5: Stop

4. Flowchart

 ┌─────────┐
 │  Start  │
 └────┬────┘
      │
      ▼
 ┌──────────────┐
 │ n=11,count=0 │
 └────┬─────────┘
      │
      ▼
 ┌────────────┐
 │ n <= 60 ?  │
 └───┬────┬───┘
     │Yes │No
     ▼    ▼
 ┌────────────┐
 │ prime=True │
 │ i = 2      │
 └────┬───────┘
      │
      ▼
 ┌──────────────┐
 │ i <= √n ?    │
 └───┬────┬─────┘
     │Yes │No
     ▼    ▼
 ┌──────────────┐
 │ n % i == 0 ? │
 └───┬────┬─────┘
     │Yes │No
     ▼    ▼
prime=False i=i+1
     │      │
     └──┬───┘
        ▼
  If prime=True
     count=count+1
        │
        ▼
      n=n+1
        │
        └──────► Back to
                n <= 60 ?
        │
        ▼
   Print count
        │
        ▼
      Stop
Enter fullscreen mode Exit fullscreen mode

5. Python Code

n = 11
count = 0

while n <= 60:
    prime = True
    i = 2

    while i <= int(n ** 0.5):
        if n % i == 0:
            prime = False
            break
        i += 1

    if prime:
        count += 1

    n += 1

print("Number of prime numbers between 11 and 60 =", count)
Enter fullscreen mode Exit fullscreen mode

6. Result

Number of prime numbers between 11 and 60 = 13
Enter fullscreen mode Exit fullscreen mode

Task-2 : 3753 is divisible by 9 and hence divisible by ______.

1. Concept

A number divisible by 9 is always divisible by 3 because 9 is a multiple of 3.

Divisibility Rule of 9:
If the sum of digits is divisible by 9, then the number is divisible by 9.

For 3753:

3 + 7 + 5 + 3 = 18

18 is divisible by 9.

Therefore, 3753 is divisible by 9 and also by 3.

2. Logic

  1. Find the sum of digits of 3753.
  2. Check whether the sum is divisible by 9.
  3. If yes, conclude that 3753 is divisible by 9.
  4. Since every number divisible by 9 is also divisible by 3, print 3.

3. Algorithm

Step 1: Start

Step 2: Assign n = 3753

Step 3: Find sum of digits

Step 4: Check if sum is divisible by 9

Step 5: If yes, display "3753 is divisible by 3"

Step 6: Stop

4. Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
┌───────────┐
│ n = 3753  │
└────┬──────┘
     │
     ▼
┌────────────────┐
│ Sum digits=18  │
└────┬───────────┘
     │
     ▼
┌───────────────┐
│18 % 9 == 0 ?  │
└───┬─────┬─────┘
    │Yes  │No
    ▼     ▼
┌──────────────┐
│ Print 3      │
└────┬─────────┘
     │
     ▼
 ┌───────┐
 │ Stop  │
 └───────┘
Enter fullscreen mode Exit fullscreen mode

5. Python Code

n = 3753
s = 0

while n > 0:
    s = s + (n % 10)
    n = n // 10

if s % 9 == 0:
    print("3753 is divisible by 9 and hence divisible by 3")
else:
    print("Not divisible by 9")
Enter fullscreen mode Exit fullscreen mode

6. Result


3753 is divisible by 9 and hence divisible by 3
Enter fullscreen mode Exit fullscreen mode

Task-3 : 4 Digit Prime Number

1. Concept

A prime number is a number greater than 1 that has exactly two factors:

  • 1
  • itself A 4-digit prime number is a prime number between 1000 and 9999 Hence 4-digit prime number is any prime number in the range: 👉 1000 to 9999

2. Logic

  1. Start from 1000.
  2. Check each number up to 9999.
  3. For each number:
  • Check if it has any divisor from 2 to √n.
  • If no divisor exists → it is prime.
    1. Print all 4-digit prime numbers.

3. Algorithm


Step 1: Start
Step 2: Initialize n = 1000
Step 3: Repeat while n ≤ 9999
    a. Set prime = True
    b. Set i = 2
    c. While i ≤ √n
        - If n % i == 0, set prime = False, break
        - Increment i
    d. If prime == True, print n
    e. Increment n
Step 4: Stop

4. Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
┌──────────────┐
│ n = 1000     │
└────┬─────────┘
     │
     ▼
┌────────────────┐
│ n ≤ 9999 ?     │
└───┬─────┬──────┘
    │Yes  │No
    ▼     ▼
┌──────────────┐
│ prime = True │
│ i = 2        │
└────┬─────────┘
     │
     ▼
┌────────────────┐
│ i ≤ √n ?       │
└───┬─────┬──────┘
    │Yes  │No
    ▼     ▼
┌────────────────┐
│ n % i == 0 ?   │
└───┬─────┬──────┘
    │Yes  │No
    ▼     ▼
prime=F  i=i+1
    │      │
    └──┬───┘
       ▼
┌────────────────┐
│ if prime print │
└────┬───────────┘
     │
     ▼
   n = n + 1
     │
     └──────► Back to
             n ≤ 9999 ?
     │
     ▼
   Stop
Enter fullscreen mode Exit fullscreen mode

5. Python Code

n = 1000

while n <= 9999:
    prime = True
    i = 2

    while i <= int(n ** 0.5):
        if n % i == 0:
            prime = False
            break
        i += 1

    if prime:
        print(n)

    n += 1
Enter fullscreen mode Exit fullscreen mode

6. Result

The program prints all 4-digit prime numbers from 1000 to 9999.

Example Output (first few and last few):

1009
1013
1019
1021
1031
1033
1039
...
9973
Enter fullscreen mode Exit fullscreen mode

Task-4 : Write the smallest and the biggest two-digit prime number.

1. Concept

A prime number is a number greater than 1 having exactly two factors:

  • 1
  • itself

Two-digit numbers range from 10 to 99
We search within **two-digit numbers (10–99)
and find:

  • First prime → smallest
  • Last prime → biggest

2. Logic

  1. Start from 10.
  2. Check each number up to 99.
  3. If number is prime:
  • If first prime found → store as smallest
  • Keep updating → last prime becomes biggest
    1. Print both values.

3. Algorithm


Step 1: Start
Step 2: Initialize n = 10
Step 3: Set smallest = 0, largest = 0
Step 4: While n ≤ 99
    a. Check if n is prime
    b. If prime:
        - If smallest == 0, assign smallest = n
        - Assign largest = n
    c. Increment n
Step 5: Print smallest and largest
Step 6: Stop

4. Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
┌────────────────────────┐
│ n=10, smallest=0       │
│ largest=0              │
└────┬───────────────────┘
     │
     ▼
┌────────────────┐
│ n ≤ 99 ?       │
└───┬─────┬──────┘
    │Yes  │No
    ▼     ▼
┌──────────────┐
│ Check prime  │
└────┬─────────┘
     │
     ▼
┌──────────────────────────┐
│ If prime?                │
└───┬─────────┬────────────┘
    │Yes      │No
    ▼         ▼
┌──────────────────────────┐
│ if smallest==0 → store   │
│ smallest = n             │
│ largest = n              │
└────────┬─────────────────┘
         │
         ▼
      n = n + 1
         │
         └──────► Back to
                 n ≤ 99 ?
         │
         ▼
┌────────────────────┐
│ Print smallest     │
│ Print largest      │
└────────┬───────────┘
         ▼
       Stop
Enter fullscreen mode Exit fullscreen mode

5. Python Code

n = 10
smallest = 0
largest = 0

while n <= 99:
    prime = True
    i = 2

    while i <= int(n ** 0.5):
        if n % i == 0:
            prime = False
            break
        i += 1

    if prime and n > 1:
        if smallest == 0:
            smallest = n
        largest = n

    n += 1

print("Smallest two-digit prime number =", smallest)
print("Biggest two-digit prime number =", largest)
Enter fullscreen mode Exit fullscreen mode

6. Result

Smallest two-digit prime number = 11
Biggest two-digit prime number = 97
Enter fullscreen mode Exit fullscreen mode

Task-5 : “I am a two-digit prime number and the sum of my digits is 10. I am also one of the factors of 57. Who am I?”

1. Concept

We need to find a number that satisfies three conditions:

  1. It is a two-digit number (10–99)
  2. It is a prime number
  3. Sum of digits = 10
  4. It is a factor of 57

Then we check which number satisfies all conditions.

2. Logic

We proceed step-by-step:

Step 1: Factors of 57

57 = 1, 3, 19, 57
So possible candidates: 3, 19

Only two-digit factor = 19

Step 2: Check conditions for 19

  • Two-digit? ✔ Yes
  • Prime? ✔ Yes
  • Sum of digits = 1 + 9 = 10 ✔ Yes
  • Factor of 57? ✔ Yes

So the number is 19

3. Algorithm

Step 1: Start
Step 2: Find factors of 57
Step 3: Select two-digit factors
Step 4: For each candidate:

  • Check if prime
  • Check sum of digits = 10 Step 5: If all conditions satisfy, print number Step 6: Stop

4. Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
┌──────────────┐
│ n = factors  │
│ of 57        │
└────┬─────────┘
     │
     ▼
┌──────────────────┐
│ Select 2-digit   │
│ numbers          │
└────┬─────────────┘
     │
     ▼
┌──────────────────┐
│ Check prime?     │
└────┬─────┬───────┘
     │Yes  │No
     ▼     ▼
┌────────────────────┐
│ sum of digits=10?  │
└────┬─────┬─────────┘
     │Yes  │No
     ▼     ▼
┌──────────────┐
│ Print number │
└────┬─────────┘
     ▼
   Stop
Enter fullscreen mode Exit fullscreen mode

5. Python Code

num = 57
i = 1
result = []

# find factors
while i <= num:
    if num % i == 0:
        result.append(i)
    i += 1

# check conditions
j = 0
answer = None

while j < len(result):
    n = result[j]

    if 10 <= n <= 99:
        prime = True
        k = 2

        while k <= int(n ** 0.5):
            if n % k == 0:
                prime = False
                break
            k += 1

        if prime:
            s = (n // 10) + (n % 10)

            if s == 10:
                answer = n

    j += 1

print("The number is:", answer)
Enter fullscreen mode Exit fullscreen mode

6. Result

The number is: 19
Enter fullscreen mode Exit fullscreen mode

Task-6 :HCF (x, y) = x, if y is a multiple of x

1. Concept

The HCF (Highest Common Factor) of two numbers is the greatest number that divides both.

If y is a multiple of x, then:

  • y = x × k (for some integer k)
  • So x divides both x and y completely
  • Hence, HCF(x, y) = x

2. Logic

  1. Input two numbers: x and y
  2. Check whether y % x == 0
  3. If true → x is HCF
  4. Otherwise → compute HCF using normal method (Euclidean idea / repeated subtraction / division)
  5. Print result

3. Algorithm

Step 1: Start
Step 2: Input x, y
Step 3: Check if y % x == 0
    → If yes, HCF = x
    → Else compute HCF using loop method
Step 4: Display HCF
Step 5: Stop

4. Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
┌──────────────┐
│ Input x, y   │
└────┬─────────┘
     │
     ▼
┌────────────────┐
│ y % x == 0 ?   │
└───┬─────┬──────┘
    │Yes  │No
    ▼     ▼
┌──────────────┐   ┌─────────────────────┐
│ HCF = x      │   │ Compute HCF using   │
└────┬─────────┘   │ repeated subtraction│
     │             └─────────┬───────────┘
     ▼                       ▼
┌──────────────┐     ┌──────────────┐
│ Print HCF    │     │ Print HCF    │
└────┬─────────┘     └────┬─────────┘
     ▼                   ▼
        ┌───────┐
        │ Stop  │
        └───────┘
Enter fullscreen mode Exit fullscreen mode

5. Python Code

Case 1: Using given condition

x = int(input("Enter x: "))
y = int(input("Enter y: "))

if y % x == 0:
    hcf = x
else:
    # Euclidean method
    a, b = x, y
    while b != 0:
        a, b = b, a % b
    hcf = a

print("HCF =", hcf)
Enter fullscreen mode Exit fullscreen mode

6. Result

Input:

x = 6
y = 24
Enter fullscreen mode Exit fullscreen mode

Output:

HCF = 6
Enter fullscreen mode Exit fullscreen mode

Task- 7 : What is the smallest 5-digit number that is exactly divisible by 72 and 108?

1. Concept

To find a number divisible by both 72 and 108, we first find their LCM (Least Common Multiple).

The smallest number divisible by both numbers is their LCM, and then we scale it to the smallest 5-digit number.
The smallest 5-digit number is: 10080

2. Step 1: Find LCM of 72 and 108

Prime factorization:

  • 72 = 2³ × 3²
  • 108 = 2² × 3³

LCM = highest powers:

= 2³ × 3³
= 8 × 27
= 216

So, LCM(72, 108) = 216

3. Logic

  1. Find LCM of 72 and 108 → 216
  2. Find smallest 5-digit number → 10000
  3. Divide 10000 by 216
  4. Round up to next integer
  5. Multiply back by 216
  6. Get smallest required number

4. Algorithm

Step 1: Start
Step 2: Compute LCM(72, 108) = 216
Step 3: Set smallest 5-digit number = 10000
Step 4: Compute quotient = 10000 ÷ 216
Step 5: Round quotient upward
Step 6: Multiply quotient × 216
Step 7: Print result
Step 8: Stop

5. Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
┌────────────────────┐
│ Find LCM = 216     │
└────┬───────────────┘
     │
     ▼
┌────────────────────┐
│ Set n = 10000      │
└────┬───────────────┘
     │
     ▼
┌────────────────────┐
│ n / 216            │
└────┬───────────────┘
     │
     ▼
┌────────────────────┐
│ Round up quotient  │
└────┬───────────────┘
     │
     ▼
┌────────────────────┐
│ Multiply by 216    │
└────┬───────────────┘
     │
     ▼
┌──────────────┐
│ Print result │
└────┬─────────┘
     ▼
   Stop
Enter fullscreen mode Exit fullscreen mode

6. Python Code

import math

# numbers
a = 72
b = 108

# compute LCM
def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

lcm = (a * b) // gcd(a, b)

# smallest 5-digit number
n = 10000

# find multiplier
k = math.ceil(n / lcm)

result = k * lcm

print("Smallest 5-digit number divisible by 72 and 108 =", result)
Enter fullscreen mode Exit fullscreen mode

7. Result

Smallest 5-digit number divisible by 72 and 108 = 10080
Enter fullscreen mode Exit fullscreen mode

Task- 8 : There are four Mobile Phones in a house. At 5 a.m, all the four Mobile Phones will ring together. Thereafter, the first one rings every 15 minutes, the second one rings every 20 minutes, the third one rings every 25 minutes and the fourth one rings every 30 minutes. At what time, will the four Mobile Phones ring together again?

1. Concept

This is a problem of LCM (Least Common Multiple).

We need to find when all four cycles match again.

  • 1st phone → every 15 min
  • 2nd phone → every 20 min
  • 3rd phone → every 25 min
  • 4th phone → every 30 min

So we calculate:
[
LCM(15, 20, 25, 30)
]

That gives the time interval after which all phones ring together.
👉 They will ring together again after 300 minutes = 5 hours

Time = 10:00 a.m.

2. Logic

  1. Find LCM of 15, 20, 25, 30
  2. Convert LCM minutes into hours
  3. Add to starting time (5:00 a.m.)
  4. Get final time

3. Algorithm

Step 1: Start
Step 2: Input 15, 20, 25, 30
Step 3: Find LCM of all numbers
Step 4: Convert LCM into minutes
Step 5: Add to 5:00 a.m.
Step 6: Display final time
Step 7: Stop

4. Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
┌────────────────────────┐
│ Intervals: 15,20,25,30 │
└────┬───────────────────┘
     │
     ▼
┌──────────────────────┐
│ Find LCM             │
└────┬─────────────────┘
     │
     ▼
┌──────────────────────┐
│ LCM = 300 minutes    │
└────┬─────────────────┘
     │
     ▼
┌──────────────────────┐
│ Add to 5:00 a.m.     │
└────┬─────────────────┘
     │
     ▼
┌──────────────────────┐
│ 10:00 a.m.           │
└────┬─────────────────┘
     │
     ▼
   Stop
Enter fullscreen mode Exit fullscreen mode

5. Python Code

import math

# function to find gcd
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

# function to find lcm
def lcm(a, b):
    return (a * b) // gcd(a, b)

# intervals
numbers = [15, 20, 25, 30]

# compute LCM of all
result = numbers[0]
i = 1

while i < len(numbers):
    result = lcm(result, numbers[i])
    i += 1

print("LCM =", result, "minutes")

start_hour = 5

# convert minutes to hours
hours_added = result // 60

final_time = start_hour + hours_added

print("All phones ring together again at:", final_time, ":00 a.m.")
Enter fullscreen mode Exit fullscreen mode

6. Result

LCM = 300 minutes  
All phones ring together again at: 10:00 a.m.
Enter fullscreen mode Exit fullscreen mode

Task- 9 : Find prime numbers from 11 to 99 using a function find_prime()

1. Concept

A prime number is a number greater than 1 that has exactly two factors:

  • 1
  • itself

We define a function find_prime() to:

  • check each number from 11 to 99
  • verify whether it is prime
  • print all prime numbers

2. Logic

  1. Create a function is_prime(n) to check primality
  2. Loop numbers from 11 to 99
  3. For each number:
  • check divisibility from 2 to √n
  • if not divisible → prime
    1. Print all prime numbers

3. Algorithm

Function: is_prime(n)

Step 1: Start
Step 2: Assume prime = True
Step 3: For i from 2 to √n
    If n % i == 0 → not prime
Step 4: Return result

Main Function: find_prime()

Step 1: Start
Step 2: For n from 11 to 99
Step 3: Call is_prime(n)
Step 4: If True → print n
Step 5: Stop

4. Flowchart

 ┌────────────┐
 │   Start    │
 └────┬───────┘
      │
      ▼
┌────────────────────┐
│ n = 11 to 99       │
└────┬───────────────┘
     │
     ▼
┌────────────────────┐
│ Call is_prime(n)   │
└────┬───────────────┘
     │
     ▼
┌────────────────────┐
│ Check divisibility │
│ up to √n           │
└────┬───────┬───────┘
     │Yes    │No
     ▼       ▼
 Not prime   Prime
     │        │
     └────┬───┘
          ▼
┌────────────────────┐
│ If prime → print n │
└────┬───────────────┘
     ▼
  next n
     │
     ▼
   Stop
Enter fullscreen mode Exit fullscreen mode

5. Python Code

def is_prime(n):
    if n < 2:
        return False

    i = 2
    while i <= int(n ** 0.5):
        if n % i == 0:
            return False
        i += 1

    return True


def find_prime():
    n = 11
    while n <= 99:
        if is_prime(n):
            print(n)
        n += 1


# call function
find_prime()
Enter fullscreen mode Exit fullscreen mode

6. Result

11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Enter fullscreen mode Exit fullscreen mode

Task- 10 : Problem Check divisibility by 19 using the condition: 57 % 19 == 0

1. Concept

A number is divisible by another number if:
[
\text{remainder} = 0
]

So, for divisibility by 19:

  • Divide 57 by 19
  • Check remainder using modulus operator %

If:
[
57 % 19 = 0
]
then 57 is divisible by 19.

2. Logic

  1. Take number = 57
  2. Take divisor = 19
  3. Compute 57 % 19
  4. If remainder = 0 → divisible
  5. Else → not divisible

3. Algorithm

Step 1: Start
Step 2: Set n = 57, d = 19
Step 3: Compute r = n % d
Step 4: If r == 0
    → Print “Divisible”
Step 5: Else
    → Print “Not divisible”
Step 6: Stop

4. Flowchart

 ┌───────┐
 │ Start │
 └───┬───┘
     │
     ▼
┌──────────────┐
│ n = 57, d=19 │
└────┬─────────┘
     │
     ▼
┌────────────────┐
│ r = n % d      │
└────┬───────────┘
     │
     ▼
┌────────────────┐
│ r == 0 ?       │
└───┬─────┬──────┘
    │Yes  │No
    ▼     ▼
┌──────────────┐  ┌──────────────┐
│ Divisible    │  │ Not Divisible│
└────┬─────────┘  └────┬─────────┘
     ▼                 ▼
          ┌───────┐
          │ Stop  │
          └───────┘
Enter fullscreen mode Exit fullscreen mode

5. Python Code

n = 57
d = 19

if n % d == 0:
    print(n, "is divisible by", d)
else:
    print(n, "is not divisible by", d)
Enter fullscreen mode Exit fullscreen mode

6. Result

57 is divisible by 19
Enter fullscreen mode Exit fullscreen mode

Top comments (0)