Python Program to find the sum of all prime numbers in an array


In this program, you will learn how to find the sum of all prime numbers in an array in Python.


a = arr.array('i', [2, 3, 4, 5])

Example: How to find the sum of all prime numbers in an array in Python

import array as arr

a = arr.array('i', [2, 3, 4, 5])

s = 0
for i in a:
    num = i
    j = 2
    f = 1
    while j < num:
        if num % j == 0:
            f = 0
            break
        j = j + 1

    if f == 1:
        s = s + num

print("Sum of all prime numbers are : ", s)

Output:

Sum of all prime numbers are :  10

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.