AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
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 169
array x 12
Posted On : 2017-07-07 16:58:04.0
profile Divesh - anyforum.in Divesh
101180
up-rate
5
down-rate

Answers


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<size;i++){
numbers[i]=input.nextInt();
}
Map<Integer, Integer> elementIndexMap=getPrimeIndex(numbers);
System.out.println(total);
total=0;
for(Map.Entry<Integer, Integer> 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<Integer, Integer> getPrimeIndex(int[] input){
Map<Integer, Integer> elementIndexMap=new HashMap<Integer, Integer>();
for(int i=0;i<input.length;i++){
if(isPrime(i) && isPrime(input[i])){
total++;
elementIndexMap.put(input[i], i);
}
}
return elementIndexMap;
}

private static boolean isPrime(int num) {
if (num < 2) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i * i <= num; i += 2)
if (num % i == 0) return false;
return true;
}
}

Posted On : 2017-07-08 22:41:38
Satisfied : 6 Yes  1 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188249150
Reply This Thread
up-rate
5
down-rate
Comments
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;
}
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  49150
Posted On :2017-07-09 23:06:57.0
Leave a Comment

import java.util.*;
class d
{
public static void main(String[] args) {
int[] array = new int[5];
int k=1;
Scanner in = new Scanner(System.in);
int count=0;
System.out.println("Enter the elements of the array: ");

for (int i = 0; i < 5; i++) {
array[i] = in.nextInt();
}
while(k<=2){
for (int i = 2; i < array.length; i++) {

boolean isPrime = true;
if (array[i] == 1)
isPrime = false;
else {

for (int j = 2; j <= array[i] / 2; j++) {
if (array[i] % j == 0) {
isPrime = false;
break;
}
}

}
if (isPrime && (i%2)!=0 || i==2 ){
if(k==1)
count++;
else
System.out.println(array[i]+" "+i);
}
}
if(k==1)
System.out.println(count);
k++;
}
}
}

Posted On : 2017-07-09 15:44:49
Satisfied : 2 Yes  1 No
profile hemana mana - anyforum.in hemana mana
1240
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in