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.
# function for test non-prime number
def non_prime(n):
for i in range(2, n):
if n % i == 0:
return True
return False
# list of numbers
numbers = []
# set variable for sum to 0
sum_non_primes=0
# input number of numbers
num=int(input('Input N>'))
# input numbers
for i in range(num):
n=int(input('Input number ' + str(i+1) + '>'))
numbers.append(n)
# test numbers and calculate sum
n=0
res=''
for nn in numbers:
if non_prime(nn):
sum_non_primes+=nn
if n>0:
res+=' + '
res+=str(nn)
n+=1
# print result
print('Sum of Non-Primes ' + res + ' is ' + str(sum_non_primes))
Comments
Leave a comment