All Courses

Prime number

By Sudipchoudhury.myapplications, 3 years ago
  • Bookmark
1

I am trying to find list of prime numbers from a list of numbers. I am not getting correct output. Looks like the problem is happening with range in second for loop.


Kindly help to build with this login only. Let me know where to modify.



length = int(input('Enter the range: '))

l=[]

for i in range(length):

  l.append(int(input(f"Element at position {i} is: ")))

print(l)

prime=[]

count = 0

for j in l:

  for k in range(1,j+1):

    if (j%k==0):

      count+=1

  if (count==2):

    prime.append(j)

print(prime)


Output:


Enter the range: 3
Element at position 0 is: 10
Element at position 1 is: 7
Element at position 2 is: 3
[10, 7, 3]
[]


Python
0 Answer
Your Answer

Webinars

More webinars

Related Discussions

Running random forest algorithm with one variable

View More