Skip to main content

Command Palette

Search for a command to run...

Prime Number Program with Python

Updated
2 min readView as Markdown
Prime Number Program with Python
S

I am learning new technologies and implementing projects.

I aimed on being consistent with my learning and focus on converting complex programs as cakewalk process.

If you like, what I write, then follow and learn along.

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

Z
Zephaniah4y ago

Great article Samra 💯

M
MCCurcio4y ago

Love Python & Primes! Are you familiar with Pythagorean Triplets?

S

Yes MCCurcio I'm familiar will the concept of Pythagorean traingle, will try to implement & share soon. :)

1
M
MCCurcio4y ago

Samra Qureshi I came across an article on how the Sumerians had carved dozens of triplets in clay several 1000 yr ago. I find the whole history and concept of triplets amazing.