Print first n prime numbers in python
Here you will get an example program to print first n prime numbers in Python programming lab.
Program code to print first n prime numbers in python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | def is_prime(number): if number <= 1: return False for i in range(2, int(number**0.5) + 1): if number % i == 0: return False return True def find_first_n_primes(n): primes = [] count = 0 number = 2 while count < n: if is_prime(number): primes.append(number) count += 1 number += 1 return primes # Test the find_first_n_primes function num_primes = int(input("How much Prime number you want to find : ")) prime_numbers = find_first_n_primes(num_primes) print("First", num_primes, "prime numbers:") print(prime_numbers) |
Output
How much Prime number you want to find : 5
First 5 prime numbers:
[2, 3, 5, 7, 11]
Check out our other Python programming examples