Python program that checks if a given number is a prime number:

Created with Sketch.

Python program that checks if a given number is a prime number:

number = int(input("Enter a number: "))

# Check if the number is prime
if number > 1:
    for i in range(2, number):
        if (number % i) == 0:
            print(number, "is not a prime number.")
            break
    else:
        print(number, "is a prime number.")
else:
    print(number, "is not a prime number.")

The program prompts the user to enter a number using the input() function, and stores the value in the variable “number”. It then uses an if-else statement to check if the number is greater than 1. If the number is greater than 1, it uses a for loop to check if the number is divisible by any number between 2 and the number itself. If the number is divisible by any number between 2 and the number itself, it is not a prime number. If the number is not divisible by any number between 2 and the number itself, it is a prime number. If the number is less than or equal to 1, it is not a prime number.

Another way to check if a number is prime is by using the math.sqrt() function and only checking for divisibility up to the square root of the number:

import math

number = int(input("Enter a number: "))

if number > 1:
    for i in range(2, int(math.sqrt(number)) + 1):
        if (number %

Leave a Reply

Your email address will not be published. Required fields are marked *