Computer Science
Write a Python function that takes a number as a parameter and checks whether the number is prime or not.
Python
Python Functions
2 Likes
Answer
def is_prime(n):
if n <= 1:
return False
factors = 0
for i in range(1, n + 1):
if n % i == 0:
factors += 1
if factors > 2:
return False
return True
num = int(input("Enter a number: "))
is_prime_num = is_prime(num)
print("Is", num, "prime?", is_prime_num)
Output
Enter a number: 4
Is 4 prime? False
Enter a number: 29
Is 29 prime? True
Answered By
1 Like
Related Questions
Write a Python function to multiply all the numbers in a list.
Sample List: (8, 2, 3, -1, 7)
Expected Output : -336Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number whose factorial is to be calculated as the argument.
Write a Python function that checks whether a passed string is a palindrome or not.
Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically.
Sample Items: green-red-yellow-black-white
Expected Result: black-green-red-white-yellow