Write a program in python to check whether a number is prime or not.

  1. Here’s a Python code to check if a given number is prime or not:


    def is_prime(n):
    if n <= 1:
    return False
    elif n <= 3:
    return True
    elif n % 2 == 0 or n % 3 == 0:
    return False
    i = 5
    while i * i <= n:
    if n % i == 0 or n % (i + 2) == 0:
    return False
    i += 6
    return True

    # Test the function
    number = int(input(“Enter a number: “))
    if is_prime(number):
    print(f”{number} is a prime number”)
    else:
    print(f”{number} is not a prime number”


Python Program

 

 

This code defines a function `is_prime()` that takes an integer `n` as input and returns `True` if `n` is a prime number, and `False` otherwise. Then, it prompts the user to enter a number, checks if it’s prime using the `is_prime()` function, and prints the result.

 

 

 

Leave a Reply

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