CBSE Class 11 Informatics Practices Question 38 of 40

Practice Paper — Question 3

Back to all questions
3
Question

Question 33(iii)

Write a program to accept a number from the user and check whether it is a prime number or not.

Solution
n = int(input("Enter a number: "))
if n <= 1:
    print(n, "is not a prime number.")
else:
    factors = 0
    for i in range(1, n + 1):
        if n % i == 0:
            factors += 1
    if factors == 2:
        print(n, "is a prime number.")
    else:
        print(n, "is not a prime number.")
Output
Enter a number: 11
11 is a prime number.

Enter a number: 24
24 is not a prime number.
Answer