DEV Community

Cover image for Python Logical Problem: Find the First Two-Digit Prime Number and Last Two-Digit Prime Number
Narasimma Radhakrishnan
Narasimma Radhakrishnan

Posted on

Python Logical Problem: Find the First Two-Digit Prime Number and Last Two-Digit Prime Number

Problem Statement:

Write a Python program to find:

  1. The first two-digit prime number.
  2. The last two-digit prime number.
first = 0
last = 0

# Find first two-digit prime
for n in range(10, 100):

    count = 0

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

    if count == 2:
        first = n
        break

# Find last two-digit prime
for n in range(99, 9, -1):

    count = 0

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

    if count == 2:
        last = n
        break

print("First Prime:", first)
print("Last Prime:", last)
Enter fullscreen mode Exit fullscreen mode
First Prime: 11
Last Prime: 97
Enter fullscreen mode Exit fullscreen mode

This problem helped me understand how loops, conditions, and the modulus operator work together to solve logical problems.

One important thing I learned is that we should not directly jump into coding. First, understand the problem, perform a dry run, develop the logic, and then convert it into a Python program.

Top comments (0)