Python - Program to find the given number is a prime or composite number

 In this post, we are going to check whether an inputted number is a prime number or a composite number.

What is a Prime number or a Composite number ? 

  A prime number is a number that has only two factors i.e, 1, and the number itself.  A composite number is a number that has more than two factors. A number is said to factor that when it divides the other number it leaves no remainder.  Generally, the factors lie between 1 and the number itself.

Pre-requisites :

  • Python introduction
  • List concept
  • Conditional statements

 Program :

 a=int(input("Enter the integer:"))
list1=[] # empty list for factors
for i in range(1, a+1):
    if (a%i==0):
        list1.append(i)
if len(list1)== 2:
    print("The given number ",a," is a prime number.")
else :
    print("The given number ",a," is a composite number.")












Explanation :

  


1. First, we need to input the integer that we need to find is prime or composite.



2.Now, we are creating the empty list where we will add the factors of the inputted number.


3. Here we are using the for loop with 'i' as the variable which assigns the 'i' value from the set of numbers given in the range i.e, from 1 to a (inputted number). The stop value is considered as n-1. So if we put (1, a), the stop value will be a-1. That's why we put (1,a+1). Here we assigning like this because the factors of a number are from 1 to the number itself.


4. Here we are checking that the 'i' value is a factor or not. The operator used here is (%) modulus which gives the remainder when 'a' is divided by 'i'. The comparison operator (==) used here is called 'equal to' returns true if LHS=RHS and else false. The 'if' succeeds the next step i.e, step 5 if the condition is true.

5.Now we are adding the factors in the empty list that we created. This step is inside if statement. If the if condition is true, the lines are executed. If the if condition is true, the 'i' is a factor of a. The append function adds the specified element in the list1. Now, the numbers which are factors of the inputted number are added to the empty list. 


6. Now we are checking the number of factors. If the number of factors is equal to 2, we know that a number is a prime number. As this line is close to the margin we know that it is outside for loop that we created already.
The function used in this statement is len() which returns the number of elements in the specified list. The operator used is equal to an operator         ( ==) which returns true if the number of elements in list1 is 2, else false. If the number of the elements in the list1 i.e, the number of factors is 2, it proceeds the step 7, else it proceeds to step 8.


7. This statement is in the if statement i.e, it proceeds only when the if the condition is true.  If the number of factors is 2, that number is a prime number. Now we are using the print statement what we said in the output. The strings which is the green part are printed as it is. The 'a' is also printed i.e, the inputted number. The parts inside the print ()are separated by commas as its syntax.


8. If the number is composite, we are giving the output here. The conditional statement used here is else. This statement is written below the if statement with proper indentation as of the if statement. The else statement only executes only if the 'if condition' fails. The 'if condition' above written is when the number of factors is 2. If the number of factors is not equal to 2, the statement below the else part executes i.e, step 9.



9. As we can see this statement is below else part. As we discussed already this print statement executes only when the 'if condition' fails. If the number of elements in list1 i.e, the number of factors of a number is not 2, then it must be a composite number. We are printing that using a print statement.

Input/Output :

Input/Output for a composite number :





Input/Output for a prime number :








     Thank you


Comments

Post a Comment

Popular posts from this blog

Python Program to Print Fibonacci series - Codicaly

Python Program To Replace The Elements of List With its Cube