In this program, we will find the sum of prime numbers between 1 to n.
Steps to solve the program
- Take starting and ending numbers as input through the user.
- Create a variable and assign its value equal to 0.
- If a number is divisible by 1 and by itself then it is a prime number.
- Use for loop with the range function to check whether a number is a prime number or not.
- Add only those numbers to the variable.
- Print the output.
lower = int(input("Enter strating number: "))
upper = int(input("Enter ending number: "))
total = 0
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
count = 0
for i in range(1, num+1):
if (num % i) == 0:
count += 1
if count == 2:
total += num
print("Total sum of prime numbers: ",total)
Output :
Enter strating number: 1
Enter ending number: 10
Prime numbers between 1 and 10 are:
Total sum of prime numbers: 17