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
Show 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
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.
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...
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 ...
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...
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 ...
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...
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...