Find the sum of prime numbers between 1 to n

In this program, we will find the sum of prime numbers between 1 to n.

Steps to solve the program
  1. Take starting and ending numbers as input through the user.
  2. Create a variable and assign its value equal to 0.
  3. If a number is divisible by 1 and by itself then it is a prime number.
  4. Use for loop with the range function to check whether a number is a prime number or not.
  5. Add only those numbers to the variable.
  6. 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
				
			

print all Prime numbers between 1 to n

find all prime factors of a number

Leave a Comment