Sum of Non-Primes
Write a program to print the sum of non-primes in the given N numbers. The numbers which are not primes are considered as non-primes.Input
The first line of input will contain a positive integer (N).
The following N lines will contain an integer in each line.Output
The output should be the sum of non-primes in the given numbers.Explanation
For example, if the given number is 5, then read the inputs in the next 5 lines and print the sum of non-primes in the given five numbers. If the given input integers in the next five lines are 8, 11, 96, 49, and 25 the output should be 8 + 96 + 49 + 25 is 178.
x = int(input("Enter N: "))
nums = []
sum = 0
def notPrime(num):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
return True
else:
return False
else:
return True
for n in range(x):
nums.append(int(input()))
for num in nums:
if(notPrime(num)):
sum = sum + num
primes = []
for n in range(x):
if(notPrime(nums[n])):
primes.append(nums[n])
for n in range(len(primes)):
print(primes[n],end="")
if n<len(primes)-1:
print(" + ",end="")
else:
print(" = ", sum)
Comments
Leave a comment