21.2k views
4 votes
Finding Prime Numbers: Write code that finds and counts the number of prime numbers between 2 and 100. The output should look similar to below: 2 is prime 3 is prime 4 is not prime … … 100 is not prime There are _______ number of primes between 2 and 100

1 Answer

2 votes

Answer:

I am writing the Python code:

count = 0 # counts number of prime numbers

def prime(n): # method to find prime numbers between 2 and 100

p = True #set p to true

for i in range(2,n): # loop in which i ranges from 2 to n

if (n%i == 0):

#if n mod i is equal to 0 means if n is completely divisible by i

p = False # this means number is not prime so set p to false

return p #function returns prime numbers

for j in range(2,101):

# loop in which j ranges from 2 to 101 to find prime and non prime #numbers in the given range

if prime(j): # calls prime method and checks if number is a prime

print (j,"is prime") # prints that number is prime

count= count + 1

#count variable increment every time a prime number is found

else: #else the number is not prime

print(j,"is not prime ") # prints that number is not prime

print("number of prime numbers between 2 and 100: ",count)

#displays the number of prime numbers between 2 and 100

Step-by-step explanation:

The program displays the same output as given in the example of the question. It prints every prime and non prime number from 2 to 100.

The method prime() computes the prime numbers in the given range from 2 to n. This method is called to find the prime numbers from 2 to 100 and the rest of the numbers which are not prime are displayed as non prime number. count variable counts the number of prime number in the given range of 2 to 100. The program along with output is attached. The output cannot be fully shown but it is understandable. Just copy and execute the above program and you can see its output.

Finding Prime Numbers: Write code that finds and counts the number of prime numbers-example-1
User Sixtease
by
4.9k points