Sum All Prime Numbers Ending With 7 From 1 100 in Python

Take a range from 1 to 100, find out the prime numbers between them.  Once the prime numbers between 1 - 100 are found your task is to find the prime numbers that end with digit 7.

Let's use a python program to find the total of prime numbers ending with 7.




Prime Number is a number that has only two factors 1 and number itself. None of the other numbers can divide the number other than 1 and the number itself.

 

Sum All Prime Numbers Ending With 7 From 1 100 in Python

 


Sum All Prime Numbers Ending With 7 From 1 100 in Python

take a integer variable named as total and initialize it to 0, Take a for loop and iterate from the range of 2 to 101 Get the temporary  index variable named as numerator.

Using another for loop iterate from 2 to numerator and Set the index as denominator try dividing the  numerator with the  denominator,  check if numerator is completely divisible by the denominator if so break the for loop.

With the help of the else part of the  for loop,  in the else block  take another if statement and check if the numerator  is having the last digit as 7.  try modulating the numerator with 10 and check if the last digit is 7 if so add the numerator to the total.

total = 0

for numerator in range(2, 101):
   for denominator in range(2, numerator):
       if numerator % denominator == 0:
           break
   else:
       if numerator % 10 == 7:
           total += numerator





 Upon completion of the both for loops,  the total variable will contain the sum of prime numbers that end with 7.  using the print statement print the total.


print("Sum of all Prime Numbers Ending with 7 is : ", end="")
print(total)





Python code
===================


total = 0

for numerator in range(2, 101):
   for denominator in range(2, numerator):
       if numerator % denominator == 0:
           break
   else:
       if numerator % 10 == 7:
           total += numerator


print("Sum of all Prime Numbers Ending with 7 is : ", end="")
print(total)





Output
=============

Sum of all Prime Numbers Ending with 7 is : 272




Conclusion
=====================

The problem statement might change to find the prime numbers that end with 6 are 8 or 9 whatever the number.

Change the value of 7 in the program accordingly, make sure you understand this code completely.


Comment down below if you have any suggestions to improve the above code.






Post a Comment

0 Comments