AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
3. Our Experts are looking for yours ?.

# programming-array: Write a program to list out all the prime index elements in an array

 In an array if the elements of an array and the index of an array are both prime, then it is considered as prime index element. Write a program to list out all the prime index elements in an array. Input Specification: First line of input will be the size of an array (2<=size<100) Second line will be the elements of an array Output Specification: Show the total number of prime index elements in the first line. Second line onwards, print two integers separated by space, first integer is the prime number and the second integer is its array index. Array index starts at zero. NOTE: Consider that the array which you take as input will contain at least one prime index element. Sample Input: 5 2 3 5 7 11 Sample Output: 2 5 2 7 3 programming x 169array x 12 Posted On : 2017-07-07 16:58:04.0 Divesh 101180 5

 import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class PrimeIndexFinder { private static int total=0; public static void main(String[] args) { try{ Scanner input=new Scanner(System.in); int size=input.nextInt(); int[] numbers=new int[size]; if(2<=size && size<100){ for(int i=0;i elementIndexMap=getPrimeIndex(numbers); System.out.println(total); total=0; for(Map.Entry entry:elementIndexMap.entrySet()){ System.out.println(entry.getKey()+" "+entry.getValue()); } }else{ throw new Exception("Invalid Input"); } }catch (Exception e) { System.out.println(e+" : "+e.getMessage()); } } public static Map getPrimeIndex(int[] input){ Map elementIndexMap=new HashMap(); for(int i=0;i
As per the code snippet posted by Hemana Mana, I think we must change for loop condition in getPrimeIndex method instead of 0 to last index we can make it 2 to last index as we know 0 and 1 is not prime index numbers. change code as below.

public static Map<Integer, Integer> getPrimeIndex(int[] input){
Map<Integer, Integer> elementIndexMap=new HashMap<Integer, Integer>();
for(int i=2;i<input.length;i++){
if(isPrime(i) && isPrime(input[i])){
total++;
elementIndexMap.put(input[i], i);
}
}
return elementIndexMap;
}
Rishi Kumar
523  1882  37097
Posted On :2017-07-09 23:06:57.0