# Python program to Print Prime Number between 1 to n

(1000 Views)

## Problem Statement:

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

### OR

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

## Program:

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

## Output:

Enter a number 10 [2, 3, 5, 7]

##### Need Help?

Looking for any Software or Tutorial?
Don't Worry, we will find it for you
Contact Now