Python Program to find the sum of prime numbers between 1 to n


In this program, You will learn how to find the sum of prime numbers between 1 to n in Python.


10th: 2 + 3 + 5 + 7 => 17

Example: How to find the sum of prime numbers between 1 to n in Python.

n = int(input("Enter number:"))

i = 2
s = 0

for i in range(i, n + 1):
    k = 2
    p = 1
    while k < i:
        if i % k == 0:
            p = 0
            break
        k = k + 1
    if p == 1:
        s = s + i
    i = i + 1

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

Output:

Enter number:10
Sum of prime numbers: 17