0

Can someone help me create a user defined list and print all the prime numbers in it using the for loop

23rd Apr 2022, 3:45 PM
anusha
anusha - avatar
10 odpowiedzi
+ 2
Language ? Python, C, C++, Java, JavaScript or any other.
23rd Apr 2022, 3:52 PM
Arnav Gumber
Arnav Gumber - avatar
+ 1
Attempts?
23rd Apr 2022, 3:47 PM
A͢J
A͢J - avatar
+ 1
anusha read the comments: in your code, it checks for input n is prime or not only.. a=[] n=int(input()) for i in range(0,n): if i<n: #no need this, it's always true l=int(input()) a.append(l) # indent it into loop, currently it's out side loop # next this code checks for n is prime or not. Print n if only it's prime if n>1: for i in range(2,n): if(n%i)==0: break else: print(n) But actually you need to check list items are prime or not.. So create function for prime check that add you code for prime in function and call the function on each item of list like for i in a : isPrime(i) And function : def isPrime(n) : #codd for prime check n Try this.. Hope it helps...
23rd Apr 2022, 4:22 PM
Jayakrishna 🇮🇳
+ 1
#0 to n prime numbers: [Python 🐍] n = int(input()) for prime in range(2,n): for i in range(2,prime): if(prime%i)==0: break else: print(prime)
23rd Apr 2022, 4:59 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
0
Sorry i didnt understand
23rd Apr 2022, 3:49 PM
anusha
anusha - avatar
0
anusha Attempts means did you try to solve if so then share your code here.
23rd Apr 2022, 3:52 PM
A͢J
A͢J - avatar
0
Language python
23rd Apr 2022, 3:53 PM
anusha
anusha - avatar
0
I did try to solve but it was incorrect The code is a=[] n=int(input()) for i in range(0,n): if i<n: l=int(input()) a.append(l) if n>1: for i in range(2,n): if(n%i)==0: break else: print(n)
23rd Apr 2022, 3:57 PM
anusha
anusha - avatar
0
Note that although it's perfectly valid Python to put an input() statement within a loop, that won't work within SL playground. Here, to get a list of numbers as input, you could read them all on one line, separated by spaces (like 15 3 8 12 7) then parse that into a list with: nums = [int(x) for x in input().strip().split()] As for searching for primes, in your inner loop, make sure only to try factors up to sqrt(n), (not up to n itself) otherwise your algorithm is extremely inefficient.
24th Apr 2022, 12:06 PM
Erik
0
Create an array that has user defined inputs and with the help of for loop, fetch all the prime numbers and print the numbers.
13th Sep 2023, 1:01 AM
Gowtham Sevathan
Gowtham Sevathan - avatar