65.2k views
0 votes
Write a program to read a number N from the user and then find the first N primes. A prime number is a number that only has two divisors, one and itself.

User Ben Toh
by
6.9k points

1 Answer

2 votes

Final answer:

To write a program that finds the first N prime numbers, you can use a loop to check each number starting from 2 if it is prime or not. Prime numbers are numbers that are divisible only by 1 and themselves.

Step-by-step explanation:

To write a program that finds the first N prime numbers, you can use a loop to check each number starting from 2 if it is prime or not. Prime numbers are numbers that are divisible only by 1 and themselves. To determine if a number is prime, you can check if it is divisible by any number from 2 to square root of that number. If it is divisible by any of those numbers, then it is not prime.

Here is an example program in Python:

def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

N = int(input('Enter the value of N: '))
count = 0
number = 2

while count < N:
if is_prime(number):
print(number)
count += 1
number += 1

This program takes an input N from the user and checks each number starting from 2 if it is prime or not. It keeps track of the count of prime numbers found and stops when the count reaches N.

User Wierob
by
7.9k points