The following program demonstrates how to Find the Sum of Array Elements at Non-Prime Index in Java.

Basically, a prime number is the one that is divided by only 1 or itself. So, if an array has elements at the index values 0,1,2,3,4,5,6,7,8,9,10,11,12,13 then the prime index are 2,3,5,7,11, and 13. The non-prime indexes are 0, 1, 4, 6, 8,9, 10, and 12. So in this program, first we find whether the current index is prime or non-prime. If it is non-prime, then we include the corresponding element in the sum.

``````class NonPrimeIndex
{
public static void main(String[] args)
{  //3 1 23 11 56 3 1 6 56 34 44
int[] arr={3,1,7,8,23,45,11,90,56,3,1,89,6,33,56,34,44,98};
System.out.println(findNonPrimeIndexSum(arr));
}
public static int findNonPrimeIndexSum(int[] arr)
{
int psum=0;
int n=arr.length;
for(int i=0;i<n;i++)
{
if(i==0 || i==1)
psum+=arr[i];
else
{
if(!isPrime(i))
{
System.out.println(i+"  "+arr[i]);
psum+=arr[i];
}
}
}
return psum;
}
public static boolean isPrime(int n)
{
boolean prime=true;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
prime=false;
break;
}
}
return prime;
}
}``````

Output