TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Check Prime Number

Python Check Prime Number for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.

<< Back to PYTHON

Python Program to Check Prime Number

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.

Let us look at the following example to understand the implementation.

Example:

# 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.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf