Q:

Python Program to Check Prime Number

belongs to collection: Python Basic Programs

0

We will write a program here in which we will check that a given number is a prime number or not.

Prime numbers:

If the natural number is greater than 1 and having no positive divisors other than 1 and the number itself etc.

For example: 3, 7, 11 etc are prime numbers.

Composite number:

Other natural numbers that are not prime numbers are called composite numbers.

For example: 4, 6, 9 etc. are composite numbers.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

# A default function for Prime checking conditions  
def PrimeChecker(a):  
    # Checking that given number is more than 1  
    if a > 1:  
        # Iterating over the given number with for loop  
        for j in range(2, int(a/2) + 1):  
            # If the given number is divisible or not  
            if (a % j) == 0:  
                print(a, "is not a prime number")  
                break  
        # Else it is a prime number  
        else:  
            print(a, "is a prime number")  
    # If the given number is 1  
    else:  
        print(a, "is not a prime number")  
# Taking an input number from the user  
a = int(input("Enter an input number:"))  
# Printing result  
PrimeChecker(a)  

Output:

Enter an input number:17
17 is a prime number

Explanation:

We have used nested if else condition to check if the number is a prime number or not.

First, we have checked if the given number is greater than 1 or not. If it is not greater than 1, then the number will directly come to the else part and print 'not a prime number.'

Now, the number will enter into for loop where we perform Iteration from 2 to number/2. Then, we used nested if else condition inside the for loop. If the number is completely divisible by 'i' then, it is not a prime number; else, the number is a prime number.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Program to Print all Prime Numbers in an In... >>
<< Python Program to Check Leap Year...