Answer to Question #176598 in Python for adhi chinna

Question #176598

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.


1
Expert's answer
2021-04-01T18:42:56-0400
# 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)) 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS