Sum of all the Composite Numbers from Odd indices of the given array | Java implementation to find the sum of all the composite numbers from odd indices of the given array ; Function to check for composite numbers ; Check if the factors are greater than 2 ; Check if the number is composite or not ; Function to print the sum of all composite numbers in the array ; Iterate for odd indices in the array ; Check if the number is composite then add it to sum ; return the sum ; Driver code
class GFG { static int composite ( int n ) { int flag = 0 ; int c = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { if ( n % j == 0 ) { c += 1 ; } } if ( c >= 3 ) flag = 1 ; return flag ; } static void odd_indices ( int arr [ ] , int n ) { int sum = 0 ; for ( int k = 0 ; k < n ; k += 2 ) { int check = composite ( arr [ k ] ) ; if ( check == 1 ) sum += arr [ k ] ; } System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 13 , 5 , 8 , 16 , 25 } ; int n = arr . length ; odd_indices ( arr , n ) ; } }