Prime Digit - Python Programming Exercise

This Python program prompts the user to input an integer and determines whether it is a prime number or not. A prime number is defined as a number greater than 1 that has no divisors other than 1 and itself. The program checks if the input integer is divisible by any number other than 1 and itself. If it is, the number is not prime; otherwise, it is prime. This check is performed by iterating through possible divisors up to the square root of the number to increase efficiency. This program is a great way to practice implementing loops and conditional statements in Python. It also highlights the importance of optimizing algorithms, as checking divisibility up to the square root significantly reduces the number of operations required to determine if a number is prime. By the end of the program, the user will know whether the input integer is a prime number or not, providing a useful tool for mathematical computations.

 Category

Mastering Flow Control

 Exercise

Prime Digit

 Objective

Develop a Python program that prompts the user for an integer and determines if it is a prime number or not.

 Example Python Exercise

 Copy Python Code
# Ask the user for an integer
num = int(input("Enter an integer: "))

# Function to check if a number is prime
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

# Determine if the number is prime
if is_prime(num):
    print(f"The number {num} is prime.")
else:
    print(f"The number {num} is not prime.")

 Output

Case 1:
Enter an integer: 7
The number 7 is prime.

Case 2:
Enter an integer: 10
The number 10 is not prime.

 Share this Python Exercise

 More Python Programming Exercises of Mastering Flow Control

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Dispense Change

    This Python program calculates the change for a purchase, using the largest possible coins or bills. The program prompts the user for the price o...

  •  Error Handling

    This Python program prompts the user for two numbers and performs the division operation. It uses a try..except block to catch potential errors such as ...

  •  Positive and Negative Numbers

    In this exercise, you will develop a Python program that prompts the user to input a number and then determines if the number is positive or n...

  •  Multiply Unless Zero

    In this exercise, you will develop a Python program that prompts the user for a number. If the number is not zero, the program will ask for a second ...

  •  Division When Non-Zero

    In this exercise, you will develop a Python program that prompts the user for two numbers. The program will display their division if the second numb...

  •  Conditional Division (Using Else)

    In this exercise, you will develop a Python program that prompts the user for two numbers. The program will display their division if the second numb...