Sum of prime numbers from m to n in Python

Problem Statement:

In the sum of prime numbers from m to n in python, we need to take two integers as input and should print the sum of prime numbers that come in the range of these two numbers including them. For example, in 2 and 7, the total sum is 17.

Code to find the sum of prime numbers from m to n in python:

n1 = int(input('Enter 1st number: '))
n2 = int(input('Enter 2nd number: '))
def prime(i):
    flag = 0
    for j in range(2, i//2+1):
        if i%j==0:
            flag = 1
    if flag==0:
        return True
    return False
total = 0
for i in range(n1, n2+1):
    if prime(i):
        total = total + i
print('Prime numbers sum between', n1, 'and', n2, 'is', total)

Output:

Sum of prime numbers from m to n in Python

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you