Write a Program to Find the average of all Prime Numbers from a Given Array?
Question
Write a Program to Find the average of all Prime Numbers from a Given Array?
Solution
Sure, here is a Python program that calculates the average of all prime numbers in a given array:
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def average_prime(arr):
prime_sum = 0
prime_count = 0
for num in arr:
if is_prime(num):
prime_sum += num
prime_count += 1
if prime_count == 0:
return 0
return prime_sum / prime_count
# Test the function
arr = [2, 3, 4, 5, 6, 7, 8, 9, 10]
print(average_prime(arr))
In this program, we first define a helper function is_prime(n) to check if a number n is prime. Then we define the main function average_prime(arr) to calculate the average of all prime numbers in the array arr. We iterate over each number in the array, and if the number is prime (checked by is_prime(num)), we add it to the sum of prime numbers and increment the count of prime numbers. Finally, we return the average of prime numbers, which is the sum of prime numbers divided by the count of prime numbers. If there are no prime numbers in the array, we return 0.
Similar Questions
Q5 - Average of All Prime Numbers in Given ArrayWrite a Program to Find the average of all Prime Numbers from a Given Array?Constraints:Input :- First Line of Input Consists of One Integer Value (Array Size). Second Line of Input Consists of few Integer Values Separated by Space (Array Elements).Output :- Print the Average of All Prime Number from the Array Elements.Constraints :- Array Size must be Greater than 3 or else Print "Invalid Array Size.". Print the Average value upto 3 Decimal points. All the Given Array Values Must be greater than ONE, or else print "Invalid Array Elements". If there is no Prime Numbers in a Given Array then Print "No Prime Numbers!".Example:Input 1 : 6 10 02 20 21 11 54Output 1: Average of Prime Numbers in a Given Array Elements is 6.500. Input 2 : 4 55 2 3 11Output 2: Average of Prime Numbers in a Given Array Elements is 5.333.Explanation:Input 1 : 6 10 02 20 21 11 54Output 1: Average of Prime Numbers in a Given Array Elements is 6.500.Explanation: Prime Numbers in a Given Array is 02 11. Sum of Prime Numbers in a Given Array Elements = 02 + 11 = 13 Average of Prime Numbers in a Given Array Elements = 13 / 2 = 6.500. Input 2 : 4 55 2 3 11Output 2: Average of Prime Numbers in a Given Array Elements is 5.333.Explanation: Prime Numbers in a Given Array is 2 3 11. Sum of Prime Numbers in a Given Array Elements = 2 + 3 + 11 = 16 Average of Prime Numbers in a Given Array Elements = 16 / 3 = 5.333.Public Test Cases:# INPUT EXPECTED OUTPUT1 610 02 20 21 11 54Average of Prime Numbers in a Given Array Elements is 6.500.2 455 2 3 11Average of Prime Numbers in a Given Array Elements is 5.333.
Initialize an array/list with five integers. Write a program to find the sumand average of these numbers
Q4 - Average Of all Array ElementsWrite a Program to Find the average of all elements in a Given Array?Constraints:Input :- First Line of Input Consists of One Integer Value (Array Size). Second Line of Input Consists of few Integer Values Separated by Space (Array Elements).Output :- Print the Average of All the Array Elements.Constraints :- Array Size must be Greater then 0 or else Print "Invalid ArRay Size.". Print the Average value upto 5 Decimal points.Example:Input 1 : 6 10 02 20 21 11 54Output 1: Average of Given Array Elements is 19.66666. Input 2 : 4 1 2 3 0Output 2: Average of Given Array Elements is 1.50000.Explanation:Input 1 : 6 10 02 20 21 11 54Output 1: 19.66666Explanation: Sum = 10 + 02 + 20 + 21 + 11 + 54 = 118 Average = 118 / 6 = 19.66666. Input 2 : 4 1 2 3 0Output 2: 1.50000.Explanation: Sum = 1 + 2 + 3 + 0 = 6 Average = 6 / 4 = 1.55555.
write a c program to print unique prime numbers in array
Write a program to print all Armstrong numbers and Prime Numbers in the range inputted by the user. Also print the total count of Prime Numbers in the given range.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.