w3resource

Python: Identify nonprime numbers between 1 to 100

Python Basic - 1: Exercise-116 with Solution

Write a Python program to identify non-prime numbers between 1 and 100 (integers). Print the non-prime numbers.

Sample Solution:

Python Code:

import math
def is_not_prime(n):
    ans = False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            ans = True
    return ans
print("Nonprime numbers between 1 to 100:")
for x in filter(is_not_prime, range(1, 101)):
    print(x)

Sample Output:

Nonprime numbers between 1 to 100:
4
6
8
9
10
12
14
15

..

90
91
92
93
94
95
96
98
99
100

Flowchart:

Flowchart: Python - Identify nonprime numbers between 1 to 100.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to generate and prints a list of numbers from 1 to 10.
Next: Write a Python program to make a request to a web page, and test the status code, also display the html code of the specified web page.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.