added 7-10
This commit is contained in:
33
basic programming/2nd task/8.py
Normal file
33
basic programming/2nd task/8.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# Q8
|
||||
|
||||
# Imports
|
||||
import math
|
||||
|
||||
# Receiving input from the user
|
||||
n = int(input("Enter the first number (n): "))
|
||||
m = int(input("Enter the second number (m): "))
|
||||
|
||||
# Ensure n is greater than m by swapping if necessary
|
||||
if m > n:
|
||||
n, m = m, n # Swap the values of n and m
|
||||
|
||||
# Variables for prime numbers and their sum
|
||||
prime_numbers = []
|
||||
sum_primes = 0
|
||||
|
||||
# Iterate through the range from m to n
|
||||
for num in range(m, n+1):
|
||||
if num > 1: # Only check numbers greater than 1
|
||||
is_prime = True # Assume the number is prime
|
||||
for i in range(2, int(math.sqrt(num)) + 1): # Check divisibility up to the square root of num
|
||||
if num % i == 0:
|
||||
is_prime = False # The number is not prime
|
||||
break
|
||||
|
||||
if is_prime:
|
||||
prime_numbers.append(num)
|
||||
sum_primes += num
|
||||
|
||||
# Print the results
|
||||
print("Prime numbers in the range:", prime_numbers)
|
||||
print("Sum of prime numbers:", sum_primes)
|
||||
Reference in New Issue
Block a user