Answer to Question #165686 in Python for mani

Question #165686

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.


1
Expert's answer
2021-02-23T04:03:00-0500
def is_prime(n):
    Flag=1
    if n <=1:
        Flag=1
    else:
        for i in range(2, n):
            if n % i == 0:
                Flag=0
    if(n==2): Flag=0
    return(Flag)


Num = [2,3,12,11,43,53,56]
Non_Prime_Nums=[]
print("Input Numbers: ",Num,end=" ")
Sum=0
for r in range(0, len(Num)):
    Flag = is_prime(Num[r])
    if(Flag==0):
        Sum = Sum + Num[r]
        Non_Prime_Nums.append(Num[r])
print("\nNon-prime numbers in the list= ",Non_Prime_Nums,end=" ")
print("Sum of non-prime numbers = %d"%Sum)

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