0% found this document useful (0 votes)
15 views3 pages

P05 - Prime Numbers From 1 To 20

Uploaded by

CRITERION II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

P05 - Prime Numbers From 1 To 20

Uploaded by

CRITERION II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5. Write a Python script that prints prime numbers less than 20.

Aim : Prints prime numbers less than 20

Procedure:

Step 1: Open Python IDE.

Step 2: Get the value for n.

Step 3: Create a for num loop from 1 to n.

Step 4: If the num variable has 1 then no action take place.

Step 5: Otherwise Create nested for i loop from 2 to (num/2).

Step 6: Within the nested i loop check num%i=0. If it is true then break the loop.

Step 7: Otherwise next iteration of the nested loop works.

Step 8: After completing nested loop the else statement print the prime number.

Step 9: Then next iteration of for num loop works till n.

Result: Python script that prints prime numbers less than 20 is done successfully.
Program:

# Write a Python script that prints prime numbers less than 20

# Note: The else block will NOT be executed if the loop is stopped
# by a break statement.

n = int(input("Enter your number: "))


print("Prime numbers between 1 and",n,"are:")
for num in range(1,n+1):
if num > 1:
for i in range(2,int(num/2)+1):
if(num%i)==0:
break
else:
print(num)
Output:
>>>
Enter your number: 20
Prime numbers between 1 and 20 are:
2
3
5
7
11
13
17
19
>>>

You might also like