Q:

using python, Create an array my_Primes that contains all the primes less than 20. Make a loop that prints these elements one by one

0

using python, Create an array my_Primes that contains all the primes less than 20. Make a loop that prints these elements one by one

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

my_Primes=[]
num=0
while num<20:
    # define a flag variable
    flag = False
    # prime numbers are greater than 1
    if num > 1:
        # check for factors
        for i in range(2, num):
            if (num % i) == 0:
                # if factor is found, set flag to True
                flag = True
                # break out of loop
                break
    # check if flag is True
    if flag==False:
        my_Primes.append(num)
    num=num+1    
print('prime numbers between 0 to less then 20 are:',my_Primes)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now