Prime Number Program with Python

Prime Number Program with Python

Hello World !

Are you familiar with the concept of prime numbers?

Prime numbers are the numbers which are only divisible by 1 or itself. There are plethora of areas where we make use of prime numbers for holistic development.

Example : Cryptography algorithm, pseudorandom number generation etc.

1. What is Prime Number :

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. Example 2, 5, 7, 11 etc.

To learn more about prime numbers refer: Prime DESCR

2. How to create prime number program in python :

Lets start with coding !

image.png

Step 1: Start by greeting the user

 print("Welcome to Prime Number Program !")

Step 2: Ask the user for input

number=int(input("Enter a number "))

Step 3: Create a variable as flag and assign False to it and start the iteration using for loop.

flag=False
#Start the condition
for i in range(2,number):
    if number%i==0:
        flag=True
        break

NOTE : If the number is completely divisible by any value of i then change the value of flag as True and bring program control out of loop using break keyword.

Step 4: If the value of flag changes to True i.e number is divisible by any number other then 1 or itself hence its not a prime number, else, its a prime number

if flag==True:
    print("Not a Prime Number")
else:
    print("Prime number")

With these steps, we have successfully create prime number program using python. That's it!

I hope you find it helpful.

You can find the copy of program on my github : Samra

Please connect with me on my LinkedIn : Samra Qureshi