Python program to Print Prime Number from 1 to n


Problem Statement:

Write a Python program to print prime numbers from 1 to n.


Write a Python program to print prime numbers in a given range.


##To print prime number between 1 to n ## Prime Number is a number which is divisible only by 1 and itself. def prime(n): prime_list = [] if n in (0,1): return "Number is not a prime number" for x in range(2,n+1): ## Loop for all the number from 2 to n ## A counter to check if the number is divisible or not inside the for loop counter=0 ## Start the loop from 2 to n-1. In range the loop is executed till n-1. Also we have given the start range. for each in range(2, x): if x % each == 0: ## checks if the number is divisible by any other number in this loop. counter += 1 ## If yes counter is incremented. ## If the counter is incremented that means the number is divisible inside the loop. If the counter is zero means it is a Prime Number. if counter == 0: prime_list.append(x) return prime_list if __name__ == '__main__': num = int(raw_input("Enter a number")) result = prime(num) print result


Enter a number 10 [2, 3, 5, 7]
10 useful python tips and tricks for programmers

Struggling to Understand Algorithm and Flowchart? Try our Notes

Want to Test Your Knowledge on Algorithm and Flowchart?


Recommended Deals End in

Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Python Program to Print all Prime Numbers in an Interval

    Print series of prime numbers in python

    Print Prime numbers up to N Tutorial