Answer to Question #164828 in Python for hemanth

Question #164828

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-02-21T20:02:09-0500
def isPrime(num):
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                return False
        else:
            return True
     
    else:
        return False
        
n = int(input("Enter N: "))
nums = []
for x in range(n):
    nums.append(int(input()))


sum = 0
for num in nums:
    if(not isPrime(num)):
        sum = sum + num
m = 0


for num in nums:
    if (num == nums[n-1]):
        if( not isPrime(num)):
            print(num, " = ", sum,end="")
    else:
        if(not isPrime(num)):
            print(num, " + ", end="")
        

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
New on Blog
APPROVED BY CLIENTS