Python – Print prime numbers up to a given limit

This article of Python deals with printing all prime numbers until a given limit is reached. That is if user gives input as 10, then all prime numbers from 1 to 10 are printed from our program/script.

So the algorithm goes as follows:

First take the upper limit input from user and save it in a variable. Then convert this into int type as the input entered by the user is stored as type string.

#take input from user
a = input("Enter any number: ")
#print(type(a))  #just for understanding

#convert str a into int a

a = int(a)  #since input functon stores always as type string
#print(type(a))  #just for understanding

Then start a loop from 1 to that upper limit number and check if each number is prime or not. So here checking prime for each number is same task or repetitive task. So to do this repetitive task we use a function to check if a number is prime or not.

for i in range(1, a+1): #means start with 1 until given user input, here a+1 because end element is left out in for loop
    prime = checkPrime(i)   #we are passing each element of for loop into a function that will check if it is prime or not
    if(prime == "y"):
        print("%d is a prime number"%(i))
    else:
        print("%d is not a prime number"%(i))

Now its upto the function what we write their. If you are familiar with prime numbers that should be easy. But I will still explain it here. A prime number is defined as a number that is divisible by 1 and itself. So in other words a number to be prime, when it is divided by any other number starting from 1 to that number will give a remainder other than zero. If we get remainder as zero when divided by any number in that range then its not a prime and we need not continue the process of checking prime.

So in programming languages to check remainder we use modulus(%) operator to get remainder of a number. When we use division(/) operator we get quotient. So the entire function will look like something below.

def checkPrime(num):    #function or method syntax use "def func_name(parameters seperated by comma)"
    for i in range(2, num):
        #print(i,num)   #to see what is happening in the loop
        if(num%i == 0):
            return 'n'
    return 'y'

Now when we join all the above pieces together. The code will look like something below.

#this program will print prime numbers upto a given limit

def checkPrime(num):    #function or method syntax use "def func_name(parameters seperated by comma)"
    for i in range(2, num):
        #print(i,num)   #to see what is happening in the loop
        if(num%i == 0):
            return 'n'
    return 'y'

#print(checkPrime(5))
#exit(0)
#take input from user
a = input("Enter any number: ")
#print(type(a))  #just for understanding

#convert str a into int a

a = int(a)  #since input functon stores always as type string
#print(type(a))  #just for understanding


#now let us print all primes until the number given by the user

for i in range(1, a+1): #means start with 1 until given user input, here a+1 because end element is left out in for loop
    prime = checkPrime(i)   #we are passing each element of for loop into a function that will check if it is prime or not
    if(prime == "y"):
        print("%d is a prime number"%(i))
    else:
        print("%d is not a prime number"%(i))

Notice that I had commented some lines that you can uncomment for your understanding purposes. If you have any doubts/suggestions/feedback comment below and let us know.

2 comments

Join the Conversation

Your email address will not be published. Required fields are marked *