# Find the sum of composite elements in array

Here given code implementation process.

``````// C Program
// Find the sum of composite elements in array
#include<stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
}
//Find all prime numbers which have smaller and equal to given number n
void sieveEratosthenes(int prime[], int n)
{
if (n <= 1)
{
//When n are invalid to prime number
return;
}
// Loop controlling variables
int i;
int j;
// Initial two numbers are not prime (0 and 1)
prime[0] = 0;
prime[1] = 0;
// Set the initial (2 to n element is prime)
for (i = 2; i <= n; ++i)
{
prime[i] = 1;
}
// Initial 0 and 1 are not prime
// We start to 2
for (i = 2; i *i <= n; ++i)
{
if (prime[i] == 1)
{
//When i is prime number
//Modify the prime status of all next multiplier of location i
for (j = i *i; j <= n; j += i)
{
prime[j] = 0;
}
}
}
}
// Return maximum element in given array
int maxElement(int arr[], int size)
{
int result = arr[0];
for (int i = 1; i < size; ++i)
{
if (arr[i] > result)
{
result = arr[i];
}
}
return result;
}
// Calculate sum of composite number which is exists in given array
void sumCompositeNo(int arr[], int size)
{
// Display array element
printf(" Array element \n");
printArray(arr, size);
int max = maxElement(arr, size);
int sum = 0;
if (max > 3)
{
int prime[max + 1];
// Calculate prime numbers
sieveEratosthenes(prime, max);
// Execute loop through by size
for (int i = 0; i < size; ++i)
{
if (arr[i] > 3 && prime[arr[i]] == 0)
{
// Sum the Composite elements
sum += arr[i];
}
}
}
printf("\n Sum of Composite number is : %d\n", sum);
}
int main(int argc, char const *argv[])
{
int arr[] = {
12 , 1 , 4 , -3 , 5 , 7 , 9 , 11 , 16
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
sumCompositeNo(arr, size);
return 0;
}``````

#### Output

`````` Array element
12  1  4  -3  5  7  9  11  16
Sum of Composite number is : 41``````
``````/*
Java Program
Find the sum of composite elements in array
*/
public class CompositeNumber
{
//Display elements of given array
public void printArray(int[] arr, int size)

{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i]);
}
}
//Find all prime numbers which have smaller and equal to given number n
public void sieveEratosthenes(boolean[] prime, int n)
{
if (n <= 1)
{
//When n are invalid to prime number
return;
}
// Loop controlling variables
int i = 0;
int j = 0;
// Initial two numbers are not prime (0 and 1)
prime[0] = false;
prime[1] = false;
// Set the initial element is prime
for (i = 2; i <= n; ++i)
{
prime[i] = true;
}
// Initial 0 and 1 are not prime
// We start to 2
for (i = 2; i * i <= n; ++i)
{
if (prime[i] == true)
{
//When i is prime number
//Modify the prime status of all next multiplier of location i
for (j = i * i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
// Return maximum element in given array
public int maxElement(int[] arr, int size)
{
int result = arr[0];
for (int i = 1; i < size; ++i)
{
if (arr[i] > result)
{
result = arr[i];
}
}
return result;
}
// Calculate sum of composite number which is exists in given array
public void sumCompositeNo(int[] arr, int size)
{
// Display array element
System.out.print(" Array element \n");
printArray(arr, size);
int max = maxElement(arr, size);
int sum = 0;
if (max > 3)
{
boolean[] prime = new boolean[max + 1];
// Calculate prime numbers
sieveEratosthenes(prime, max);
// Execute loop through by size
for (int i = 0; i < size; ++i)
{
if (arr[i] > 3 && prime[arr[i]] == false)
{
// Sum the Composite elements
sum += arr[i];
}
}
}
System.out.print("\n Sum of Composite number is : " + sum + "\n");
}
public static void main(String[] args)
{
CompositeNumber task = new CompositeNumber(); // n = 3
int[] arr = {
12 , 1 , 4 , -3 , 5 , 7 , 9 , 11 , 16
};
// Get the size
int size = arr.length;
}
}``````

#### Output

`````` Array element
12  1  4  -3  5  7  9  11  16
Sum of Composite number is : 41``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Find the sum of composite elements in array
*/
class CompositeNumber
{
public:
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
}
//Find all prime numbers which have smaller and equal to given number n
void sieveEratosthenes(bool prime[], int n)
{
if (n <= 1)
{
//When n are invalid to prime number
return;
}
// Loop controlling variables
int i = 0;
int j = 0;
// Initial two numbers are not prime (0 and 1)
prime[0] = false;
prime[1] = false;
// Set the initial element is prime
for (i = 2; i <= n; ++i)
{
prime[i] = true;
}
// Initial 0 and 1 are not prime
// We start to 2
for (i = 2; i *i <= n; ++i)
{
if (prime[i] == true)
{
//When i is prime number
//Modify the prime status of all next multiplier of location i
for (j = i *i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
// Return maximum element in given array
int maxElement(int arr[], int size)
{
int result = arr[0];
for (int i = 1; i < size; ++i)
{
if (arr[i] > result)
{
result = arr[i];
}
}
return result;
}
// Calculate sum of composite number which is exists in given array
void sumCompositeNo(int arr[], int size)
{
// Display array element
cout << " Array element \n";
this->printArray(arr, size);
int max = this->maxElement(arr, size);
int sum = 0;
if (max > 3)
{
bool prime[max + 1];
// Calculate prime numbers
this->sieveEratosthenes(prime, max);
// Execute loop through by size
for (int i = 0; i < size; ++i)
{
if (arr[i] > 3 && prime[arr[i]] == false)
{
// Sum the Composite elements
sum += arr[i];
}
}
}
cout << "\n Sum of Composite number is : " << sum << "\n";
}
};
int main()
{
// n = 3
int arr[] = {
12 , 1 , 4 , -3 , 5 , 7 , 9 , 11 , 16
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}``````

#### Output

`````` Array element
12  1  4  -3  5  7  9  11  16
Sum of Composite number is : 41``````
``````// Include namespace system
using System;
/*
C# Program
Find the sum of composite elements in array
*/
public class CompositeNumber
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
}
//Find all prime numbers which have smaller and equal to given number n
public void sieveEratosthenes(Boolean[] prime, int n)
{
if (n <= 1)
{
//When n are invalid to prime number
return;
}
// Loop controlling variables
int i = 0;
int j = 0;
// Initial two numbers are not prime (0 and 1)
prime[0] = false;
prime[1] = false;
// Set the initial element is prime
for (i = 2; i <= n; ++i)
{
prime[i] = true;
}
// Initial 0 and 1 are not prime
// We start to 2
for (i = 2; i * i <= n; ++i)
{
if (prime[i] == true)
{
//When i is prime number
//Modify the prime status of all next multiplier of location i
for (j = i * i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
// Return maximum element in given array
public int maxElement(int[] arr, int size)
{
int result = arr[0];
for (int i = 1; i < size; ++i)
{
if (arr[i] > result)
{
result = arr[i];
}
}
return result;
}
// Calculate sum of composite number which is exists in given array
public void sumCompositeNo(int[] arr, int size)
{
// Display array element
Console.Write(" Array element \n");
printArray(arr, size);
int max = maxElement(arr, size);
int sum = 0;
if (max > 3)
{
Boolean[] prime = new Boolean[max + 1];
// Calculate prime numbers
sieveEratosthenes(prime, max);
// Execute loop through by size
for (int i = 0; i < size; ++i)
{
if (arr[i] > 3 && prime[arr[i]] == false)
{
// Sum the Composite elements
sum += arr[i];
}
}
}
Console.Write("\n Sum of Composite number is : " + sum + "\n");
}
public static void Main(String[] args)
{
// n = 3
int[] arr = {
12 , 1 , 4 , -3 , 5 , 7 , 9 , 11 , 16
};
// Get the size
int size = arr.Length;
}
}``````

#### Output

`````` Array element
12  1  4  -3  5  7  9  11  16
Sum of Composite number is : 41``````
``````<?php
/*
Php Program
Find the sum of composite elements in array
*/
class CompositeNumber
{
//Display elements of given array
public	function printArray( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
}
//Find all prime numbers which have smaller and equal to given number n
public	function sieveEratosthenes( & \$prime, \$n)
{
if (\$n <= 1)
{
//When n are invalid to prime number
return;
}
// Loop controlling variables
\$i = 0;
\$j = 0;
// Initial two numbers are not prime (0 and 1)
\$prime[0] = false;
\$prime[1] = false;
// Initial 0 and 1 are not prime
// We start to 2
for (\$i = 2; \$i * \$i <= \$n; ++\$i)
{
if (\$prime[\$i] == true)
{
//When i is prime number
//Modify the prime status of all next multiplier of location i
for (\$j = \$i * \$i; \$j <= \$n; \$j += \$i)
{
\$prime[\$j] = false;
}
}
}
}
// Return maximum element in given array
public	function maxElement( & \$arr, \$size)
{
\$result = \$arr[0];
for (\$i = 1; \$i < \$size; ++\$i)
{
if (\$arr[\$i] > \$result)
{
\$result = \$arr[\$i];
}
}
return \$result;
}
// Calculate sum of composite number which is exists in given array
public	function sumCompositeNo( & \$arr, \$size)
{
// Display array element
echo " Array element \n";
\$this->printArray(\$arr, \$size);
\$max = \$this->maxElement(\$arr, \$size);
\$sum = 0;
if (\$max > 3)
{
\$prime = array_fill(0, \$max + 1, true);
// Calculate prime numbers
\$this->sieveEratosthenes(\$prime, \$max);
// Execute loop through by size
for (\$i = 0; \$i < \$size; ++\$i)
{
if (\$arr[\$i] > 3 && \$prime[\$arr[\$i]] == false)
{
// Sum the Composite elements
\$sum += \$arr[\$i];
}
}
}
echo "\n Sum of Composite number is : ". \$sum ."\n";
}
}

function main()
{
// n = 3
\$arr = array(12, 1, 4, -3, 5, 7, 9, 11, 16);
// Get the size
\$size = count(\$arr);
}
main();``````

#### Output

`````` Array element
12  1  4  -3  5  7  9  11  16
Sum of Composite number is : 41``````
``````/*
Node Js Program
Find the sum of composite elements in array
*/
class CompositeNumber
{
//Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
}
//Find all prime numbers which have smaller and equal to given number n
sieveEratosthenes(prime, n)
{
if (n <= 1)
{
//When n are invalid to prime number
return;
}
// Loop controlling variables
var i = 0;
var j = 0;
// Initial two numbers are not prime (0 and 1)
prime[0] = false;
prime[1] = false;
// Initial 0 and 1 are not prime
// We start to 2
for (i = 2; i * i <= n; ++i)
{
if (prime[i] == true)
{
//When i is prime number
//Modify the prime status of all next multiplier of location i
for (j = i * i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
// Return maximum element in given array
maxElement(arr, size)
{
var result = arr[0];
for (var i = 1; i < size; ++i)
{
if (arr[i] > result)
{
result = arr[i];
}
}
return result;
}
// Calculate sum of composite number which is exists in given array
sumCompositeNo(arr, size)
{
// Display array element
process.stdout.write(" Array element \n");
this.printArray(arr, size);
var max = this.maxElement(arr, size);
var sum = 0;
if (max > 3)
{
var prime = Array(max + 1).fill(true);
// Calculate prime numbers
this.sieveEratosthenes(prime, max);
// Execute loop through by size
for (var i = 0; i < size; ++i)
{
if (arr[i] > 3 && prime[arr[i]] == false)
{
// Sum the Composite elements
sum += arr[i];
}
}
}
process.stdout.write("\n Sum of Composite number is : " + sum + "\n");
}
}

function main()
{
// n = 3
var arr = [12, 1, 4, -3, 5, 7, 9, 11, 16];
// Get the size
var size = arr.length;
}
main();``````

#### Output

`````` Array element
12  1  4  -3  5  7  9  11  16
Sum of Composite number is : 41``````
``````#  Python 3 Program
#  Find the sum of composite elements in array

class CompositeNumber :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

# Find all prime numbers which have smaller and equal to given number n
def sieveEratosthenes(self, prime, n) :
if (n <= 1) :
# When n are invalid to prime number
return

#  Initial two numbers are not prime (0 and 1)
prime[0] = False
prime[1] = False
#  Initial 0 and 1 are not prime
#  We start to 2
#  Loop controlling variables
i = 2
j = 0
while (i * i <= n) :
if (prime[i] == True) :
# When i is prime number
# Modify the prime status of all next multiplier of location i
j = i * i
while (j <= n) :
prime[j] = False
j += i

i += 1

#  Return maximum element in given array
def maxElement(self, arr, size) :
result = arr[0]
i = 1
while (i < size) :
if (arr[i] > result) :
result = arr[i]

i += 1

return result

#  Calculate sum of composite number which is exists in given array
def sumCompositeNo(self, arr, size) :
#  Display array element
print(" Array element ")
self.printArray(arr, size)
max = self.maxElement(arr, size)
sum = 0
if (max > 3) :
prime = [True] * (max + 1)
#  Calculate prime numbers
self.sieveEratosthenes(prime, max)
#  Execute loop through by size
i = 0
while (i < size) :
if (arr[i] > 3 and prime[arr[i]] == False) :
#  Sum the Composite elements
sum += arr[i]

i += 1

print("\n Sum of Composite number is : ", sum )

def main() :
#  n = 3
arr = [12, 1, 4, -3, 5, 7, 9, 11, 16]
#  Get the size
size = len(arr)

if __name__ == "__main__": main()``````

#### Output

`````` Array element
12   1   4   -3   5   7   9   11   16
Sum of Composite number is :  41``````
``````#   Ruby Program
#   Find the sum of composite elements in array

class CompositeNumber
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

end

# Find all prime numbers which have smaller and equal to given number n
def sieveEratosthenes(prime, n)
if (n <= 1)
# When n are invalid to prime number
return
end

#  Initial two numbers are not prime (0 and 1)
prime[0] = false
prime[1] = false
#  Initial 0 and 1 are not prime
#  We start to 2
#  Loop controlling variables
i = 2
j = 0
while (i * i <= n)
if (prime[i] == true)
# When i is prime number
# Modify the prime status of all next multiplier of location i
j = i * i
while (j <= n)
prime[j] = false
j += i
end

end

i += 1
end

end

#  Return maximum element in given array
def maxElement(arr, size)
result = arr[0]
i = 1
while (i < size)
if (arr[i] > result)
result = arr[i]
end

i += 1
end

return result
end

#  Calculate sum of composite number which is exists in given array
def sumCompositeNo(arr, size)
#  Display array element
print(" Array element \n")
self.printArray(arr, size)
max = self.maxElement(arr, size)
sum = 0
if (max > 3)
prime = Array.new(max + 1) {true}
#  Calculate prime numbers
i = 0
self.sieveEratosthenes(prime, max)
#  Execute loop through by size
while (i < size)
if (arr[i] > 3 && prime[arr[i]] == false)
#  Sum the Composite elements
sum += arr[i]
end

i += 1
end

end

print("\n Sum of Composite number is : ", sum ,"\n")
end

end

def main()
#  n = 3
arr = [12, 1, 4, -3, 5, 7, 9, 11, 16]
#  Get the size
size = arr.length
end

main()``````

#### Output

`````` Array element
12  1  4  -3  5  7  9  11  16
Sum of Composite number is : 41
``````
``````/*
Scala Program
Find the sum of composite elements in array
*/
class CompositeNumber
{
//Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
}
//Find all prime numbers which have smaller and equal to given number n
def sieveEratosthenes(prime: Array[Boolean], n: Int): Unit = {
if (n <= 1)
{
//When n are invalid to prime number
return;
}
// Initial two numbers are not prime (0 and 1)
prime(0) = false;
prime(1) = false;
// Initial 0 and 1 are not prime
// We start to 2
// Loop controlling variables
var i: Int = 2;
var j: Int = 0;
while (i * i <= n)
{
if (prime(i) == true)
{
//When i is prime number
//Modify the prime status of all next multiplier of location i
j = i * i;
while (j <= n)
{
prime(j) = false;
j += i;
}
}
i += 1;
}
}
// Return maximum element in given array
def maxElement(arr: Array[Int], size: Int): Int = {
var result: Int = arr(0);
var i: Int = 1;
while (i < size)
{
if (arr(i) > result)
{
result = arr(i);
}
i += 1;
}
return result;
}
// Calculate sum of composite number which is exists in given array
def sumCompositeNo(arr: Array[Int], size: Int): Unit = {
// Display array element
print(" Array element \n");
this.printArray(arr, size);
var max: Int = this.maxElement(arr, size);
var sum: Int = 0;
if (max > 3)
{
var prime: Array[Boolean] = Array.fill[Boolean](max + 1)(true);
// Calculate prime numbers
var i: Int = 0;
this.sieveEratosthenes(prime, max);
// Execute loop through by size
while (i < size)
{
if (arr(i) > 3 && prime(arr(i)) == false)
{
// Sum the Composite elements
sum += arr(i);
}
i += 1;
}
}
print("\n Sum of Composite number is : " + sum + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: CompositeNumber = new CompositeNumber();
// n = 3
var arr: Array[Int] = Array(12, 1, 4, -3, 5, 7, 9, 11, 16);
// Get the size
var size: Int = arr.length;
}
}``````

#### Output

`````` Array element
12  1  4  -3  5  7  9  11  16
Sum of Composite number is : 41``````
``````/*
Swift 4 Program
Find the sum of composite elements in array
*/
class CompositeNumber
{
//Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
}
//Find all prime numbers which have smaller and equal to given number n
func sieveEratosthenes(_ prime: inout[Bool], _ n: Int)
{
if (n <= 1)
{
//When n are invalid to prime number
return;
}
// Initial two numbers are not prime (0 and 1)
prime[0] = false;
prime[1] = false;
// Initial 0 and 1 are not prime
// We start to 2
// Loop controlling variables
var i: Int = 2;
var j: Int = 0;
while (i * i <= n)
{
if (prime[i] == true)
{
//When i is prime number
//Modify the prime status of all next multiplier of location i
j = i * i;
while (j <= n)
{
prime[j] = false;
j += i;
}
}
i += 1;
}
}
// Return maximum element in given array
func maxElement(_ arr: [Int], _ size: Int)->Int
{
var result: Int = arr[0];
var i: Int = 1;
while (i < size)
{
if (arr[i] > result)
{
result = arr[i];
}
i += 1;
}
return result;
}
// Calculate sum of composite number which is exists in given array
func sumCompositeNo(_ arr: [Int], _ size: Int)
{
// Display array element
print(" Array element ");
self.printArray(arr, size);
let max: Int = self.maxElement(arr, size);
var sum: Int = 0;
if (max > 3)
{
var prime: [Bool] = Array(repeating: true, count: max + 1);
// Calculate prime numbers
var i: Int = 0;
self.sieveEratosthenes(&prime, max);
// Execute loop through by size
while (i < size)
{
if (arr[i] > 3 && prime[arr[i]] == false)
{
// Sum the Composite elements
sum += arr[i];
}
i += 1;
}
}
print("\n Sum of Composite number is : ", sum );
}
}
func main()
{
// n = 3
let arr: [Int] = [12, 1, 4, -3, 5, 7, 9, 11, 16];
// Get the size
let size: Int = arr.count;
}
main();``````

#### Output

`````` Array element
12   1   4   -3   5   7   9   11   16
Sum of Composite number is :  41``````
``````/*
Kotlin Program
Find the sum of composite elements in array
*/
class CompositeNumber
{
//Display elements of given array
fun printArray(arr: Array <Int> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
}
//Find all prime numbers which have smaller and equal to given number n
fun sieveEratosthenes(prime: Array <Boolean> , n: Int): Unit
{
if (n <= 1)
{
//When n are invalid to prime number
return;
}
// Initial two numbers are not prime (0 and 1)
prime[0] = false;
prime[1] = false;
// Initial 0 and 1 are not prime
// We start to 2
// Loop controlling variables
var i: Int = 2;
var j: Int ;
while (i * i <= n)
{
if (prime[i] == true)
{
//When i is prime number
//Modify the prime status of all next multiplier of location i
j = i * i;
while (j <= n)
{
prime[j] = false;
j += i;
}
}
i += 1;
}
}
// Return maximum element in given array
fun maxElement(arr: Array <Int> , size: Int): Int
{
var result: Int = arr[0];
var i: Int = 1;
while (i < size)
{
if (arr[i] > result)
{
result = arr[i];
}
i += 1;
}
return result;
}
// Calculate sum of composite number which is exists in given array
fun sumCompositeNo(arr: Array <Int> , size: Int): Unit
{
// Display array element
print(" Array element \n");
this.printArray(arr, size);
var max: Int = this.maxElement(arr, size);
var sum: Int = 0;
if (max > 3)
{
var prime: Array <Boolean> = Array(max + 1)
{
true
};
// Calculate prime numbers
var i: Int = 0;
this.sieveEratosthenes(prime, max);
// Execute loop through by size
while (i < size)
{
if (arr[i] > 3 && prime[arr[i]] == false)
{
// Sum the Composite elements
sum += arr[i];
}
i += 1;
}
}
print("\n Sum of Composite number is : " + sum + "\n");
}
}
fun main(args: Array < String > ): Unit
{
// n = 3
var arr: Array < Int > = arrayOf(12, 1, 4, -3, 5, 7, 9, 11, 16);
// Get the size
var size: Int = arr.count();
}``````

#### Output

`````` Array element
12  1  4  -3  5  7  9  11  16
Sum of Composite number is : 41``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.