Question #254619

Write an algorithm to find the sum of all the prime numbers in the given range

Expert's answer


An Algorithm to find the sum of all the prime numbers in the given range


step 1 − START

step 2declare three integers lower_range_limit, higher_range_limit & sum

step 3define values of lower_range_limit, higher_range_limit & initialize sum to 0

step 4for range between lower_range_limit &  check if a number is prime,
        if number is prime add it sum

step 5print sum

step 6 − STOP



PYTHON PROGRAM IMPLEMENTING THE ABOVE ALGORITHM


#A program to implement an algorithm to find the sum of all
# the prime numbers in the given range.
#The lower range limit and high limit are all inclusive
lower_range_limit = int(input("Enter the lower range limit: "))
higher_range_limit = int(input("Enter the higher range limit: "))
#declare a variable to store sum
sum = 0
for i in range(lower_range_limit,(higher_range_limit+1)):
    #check if the number is a prime number
    #declare a variable to show that the number is  prime by setting to 1
    prime = 1;
    if i != 1:
        for j in range(2, (i // 2)+1):
            if (i % j) == 0:
                #set prime to 0 to show that the number is not prime
                prime = 0;
                break
        if prime == 1:
            sum = sum + i

print("The sum of the prime numbers in the range "+str(lower_range_limit)+
      " to "+str(higher_range_limit)+" inclusive is "+str(sum))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS