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.
Question
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.
Solution
Here is a step-by-step solution to the problem:
-
First, we need to check if the size of the array is greater than 3. If not, we print "Invalid Array Size." and end the program.
-
Next, we need to check if all the elements in the array are greater than 1. If not, we print "Invalid Array Elements." and end the program.
-
Now, we need to find all the prime numbers in the array. A prime number is a number that has only two distinct positive divisors: 1 and itself. So, we iterate through each element in the array and check if it is a prime number. To check if a number is prime, we can iterate from 2 to the square root of the number and check if the number is divisible by any of these. If it is, then it is not a prime number.
-
We keep a sum of all the prime numbers and also count the number of prime numbers.
-
Finally, we calculate the average of the prime numbers by dividing the sum by the count. If the count is 0, i.e., there are no prime numbers, we print "No Prime Numbers!".
-
We print the average up to 3 decimal points.
This is the general algorithm to solve the problem. The actual implementation would depend on the programming language you are using.
Similar Questions
Write a Program to Find the average of all Prime Numbers from a Given Array?
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.
Initialize an array/list with five integers. Write a program to find the sumand average of these numbers
Problem StatementWrite a program to find the average of N numbers. Print the average in 2 digit precision DO NOT USE ARRAYInput FormatFirst line consist of a single integer - 'N'Second line consist of N space separated integersOutput FormatDisplay the average of N numbersSample Input51 2 3 4 5Sample Output3.00
write a c program to print unique prime numbers in array
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.