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.
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)
Comments
Leave a comment