Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Problems of Arrays in java

How to find the sum and average of nth prime in array using java
by

1 Answer

sam5epi0l
Here is an example of finding sum and average of array of prime numbers

int[] arr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; // array of prime no.
int n = 4; // nth prime
int prime = arr[n-1]; // arrays are zero-indexed
int sum = Arrays.stream(arr).limit(n).sum(); // sum of the first n primes in the array
double average = (double) sum / n; // average of the first n primes in the array

Login / Signup to Answer the Question.