Write a program to read an integer array and find the average of the numbers located on the Prime location(indexes).
- Round the average to two decimal places.
- Assume that the array starts with index 0.
- The return type (double) should be the average
Input and Output Format:
- Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next ‘n’ integers correspond to the elements in the array.
- Output consists of a single Double value.
Note: Assume that the maximum number of elements in the array is 20.
Refer to sample output for formatting specifications
Sample Input 1:
8
4
1
7
6
5
8
6
9
Sample Output 1:
7.5
Find the average of prime locations in Java
- Input the size of an array. Create an array of size ‘n’.
- Add elements to an array. Pass them to display() method.
- Inside the method, declare and initialize variable count, sum, n1, count1,avg, and d to 0.
- Use nested for loop, start the outer loop index from 2 and check if the index is a prime number. If it is a prime number then increment the count value.
- If the count value is 2, then add the element at the ith index to the sum variable.
- At last, calculate the average and use the decimal format to round the decimal places up to 2 digits.
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] a = new int[20]; int n = sc.nextInt(); for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } System.out.print(display(n, a)); } public static double display(int n, int[] a) { int count = 0, sum = 0, n1 = 0, d = 0; int count1 = 0; double avg = 0; for (int i = 2; i < n; i++) { count = 0; for (int j = 1; j <= i; j++) { if (i % j == 0) { count++; } } if (count == 2) { sum = sum + a[i]; count1++; } } avg = (double) (sum) / count1; DecimalFormat df = new DecimalFormat("#.00"); double ddd = Double.parseDouble(df.format(avg)); return ddd; } }
Output
Java program to find the location of the Prime Number
Write a code to find the location of the given prime number in the series of Prime Numbers. Consider a number ‘N’ whose location we have to find. So, if we enter N = 5, then the location of the prime number would be 3.
Input and Output Format:
- Input consists of the number ‘N’.
- Output consists of locations
Refer to sample output for formatting specifications
Sample Input :
5[Prime Number]
Sample Output :
3[Location]
Following are the steps to find the prime locations:
- Input a prime number from the user.
- Create an array of locations and initialize the loc variable to 0.
- Using the for loop, we will get all the prime numbers along with their location.
- At last, just get the location of the element from an array.
package com.demo; import java.text.DecimalFormat; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int locations[] = new int[1000]; int loc = 0; for (int i = 2; i < 100; i++) { if (locations[i] == 0) { locations[i] = ++loc; for (int j = i * 2; j <= 100; j= j+i) locations[j] = -1; } } System.out.println(locations[n]); } }
Output
Thus, in this way, we learn how to find the average of all the prime locations. Along with finding the location of prime numbers.