KnowledgeBoat Logo

Computer Science

Write a method in Python to find and display the prime numbers from 2 to N. The value of N should be passed as an argument to the method.

Python

Python Functions

1 Like

Answer

def find_primes(N):
    primes = [] 
    for num in range(2, N + 1):  
        factors = 0  
        for i in range(1, num + 1):  
            if num % i == 0:  
                factors += 1 
        if factors == 2:  
            primes.append(num)  
    return primes

N = int(input("Enter the value of N: "))
prime_numbers = find_primes(N)
print("Prime numbers from 2 to", N, "are:", prime_numbers)

Output

Enter the value of N: 10
Prime numbers from 2 to 10 are: [2, 3, 5, 7]


Enter the value of N: 30
Prime numbers from 2 to 30 are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Answered By

2 Likes


Related Questions