PRINT FIRST PRIME NUMBER USING "BREAK"

Question and sample input :

number_of_digits = int(input())

prime = 0
count = 0
for num in range(1, number_of_digits):  
    given_input = int(input())
    for b in range(2, given_input):
        if (given_input % b == 0):
            count += 1
        else:
            prime = prime + given_input
            break
    print(prime)           ```

what is the error in my code!?

here in this code, if a number is not divisible by 2 it is considered as PRIME

1 Like

But prime number means a number is only divisible by itself and one.

9, 21 and 25 were also not divisible by 2. but they are not the prime numbers.

This is not close to correct. I suggest that you first try to make your program work on any single prime number. Give it a variety of prime and non-prime inputs and see if you can get it printing only primes.

Here are the first 15 primes:
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47

1 is not a prime number - make sure your program gets this right, even if you need to treat it as a special case.

Additional but important error:

for num in range(1, number_of_digits):  

will loop one fewer time than you need. Just use range(number_of_digits).

1 Like

Can you write down the code if possible!?

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

number_of_digits = int(input())

numbers=[]
for num in range( number_of_digits):
    numbers.append(int(input()))

for num in numbers:
    if(isPrime(num)):
        print(num)
        break
1 Like