Posted on by Kalkicode
Code Array

Sum of elements in an array

Here given code implementation process.

// C Program 
// Sum of elements in an 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]);
	}
	printf("\n");
}
// Returns the sum of all integer elements in given array
int sumElement(int arr[], int size)
{
	// Define resultant variable with default zero value
	int result = 0;
	// Execute Loop through of array size
	for (int i = 0; i < size; ++i)
	{
		// Add array elements
		result += arr[i];
	}
	return result;
}
int main(int argc, char
	const *argv[])
{
	// Define array of integer elements
	int arr[] = {
		3 , 1 , -3 , 4 , 2 , 7
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	printf("  Array Element \n");
	printArray(arr, size);
	printf("  Sum of elements :  %d\n", sumElement(arr, size));
	return 0;
}

Output

  Array Element
  3  1  -3  4  2  7
  Sum of elements :  14
/*
    Java Program
    Sum of elements in an array
*/
public class Addition
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// Returns the sum of all integer elements in given array
	public int sumElement(int[] arr, int size)
	{
		// Define resultant variable with default zero value
		int result = 0;
		// Execute Loop through of array size
		for (int i = 0; i < size; ++i)
		{
			// Add array elements
			result += arr[i];
		}
		return result;
	}
	public static void main(String[] args)
	{
		Addition task = new Addition();
		// Define array of integer elements
		int[] arr = {
			3 , 1 , -3 , 4 , 2 , 7
		};
		// Get the size
		int size = arr.length;
		System.out.print(" Array Element \n");
		task.printArray(arr, size);
		System.out.print(" Sum of elements : " + task.sumElement(arr, size) + "\n");
	}
}

Output

 Array Element
  3  1  -3  4  2  7
 Sum of elements : 14
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Sum of elements in an array
*/
class Addition
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	// Returns the sum of all integer elements in given array
	int sumElement(int arr[], int size)
	{
		// Define resultant variable with default zero value
		int result = 0;
		// Execute Loop through of array size
		for (int i = 0; i < size; ++i)
		{
			// Add array elements
			result += arr[i];
		}
		return result;
	}
};
int main()
{
	Addition task = Addition();
	// Define array of integer elements
	int arr[] = {
		3 , 1 , -3 , 4 , 2 , 7
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << " Array Element \n";
	task.printArray(arr, size);
	int sum = task.sumElement(arr, size);
	cout << " Sum of elements : " << sum << "\n";
	return 0;
}

Output

 Array Element
  3  1  -3  4  2  7
 Sum of elements : 14
// Include namespace system
using System;
/*
    C# Program
    Sum of elements in an array
*/
public class Addition
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// Returns the sum of all integer elements in given array
	public int sumElement(int[] arr, int size)
	{
		// Define resultant variable with default zero value
		int result = 0;
		// Execute Loop through of array size
		for (int i = 0; i < size; ++i)
		{
			// Add array elements
			result += arr[i];
		}
		return result;
	}
	public static void Main(String[] args)
	{
		Addition task = new Addition();
		// Define array of integer elements
		int[] arr = {
			3 , 1 , -3 , 4 , 2 , 7
		};
		// Get the size
		int size = arr.Length;
		Console.Write(" Array Element \n");
		task.printArray(arr, size);
		int sum = task.sumElement(arr, size);
		Console.Write(" Sum of elements : " + sum + "\n");
	}
}

Output

 Array Element
  3  1  -3  4  2  7
 Sum of elements : 14
<?php
/*
    Php Program
    Sum of elements in an array
*/
class Addition
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Returns the sum of all integer elements in given array
	public	function sumElement( & $arr, $size)
	{
		// Define resultant variable with default zero value
		$result = 0;
		// Execute Loop through of array size
		for ($i = 0; $i < $size; ++$i)
		{
			// Add array elements
			$result += $arr[$i];
		}
		return $result;
	}
}

function main()
{
	$task = new Addition();
	// Define array of integer elements
	$arr = array(3, 1, -3, 4, 2, 7);
	// Get the size
	$size = count($arr);
	echo " Array Element \n";
	$task->printArray($arr, $size);
	$sum = $task->sumElement($arr, $size);
	echo " Sum of elements : ". $sum ."\n";
}
main();

Output

 Array Element
  3  1  -3  4  2  7
 Sum of elements : 14
/*
    Node Js Program
    Sum of elements in an array
*/
class Addition
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Returns the sum of all integer elements in given array
	sumElement(arr, size)
	{
		// Define resultant variable with default zero value
		var result = 0;
		// Execute Loop through of array size
		for (var i = 0; i < size; ++i)
		{
			// Add array elements
			result += arr[i];
		}
		return result;
	}
}

function main()
{
	var task = new Addition();
	// Define array of integer elements
	var arr = [3, 1, -3, 4, 2, 7];
	// Get the size
	var size = arr.length;
	process.stdout.write(" Array Element \n");
	task.printArray(arr, size);
	var sum = task.sumElement(arr, size);
	process.stdout.write(" Sum of elements : " + sum + "\n");
}
main();

Output

 Array Element
  3  1  -3  4  2  7
 Sum of elements : 14
#  Python 3 Program
#  Sum of elements in an array

class Addition :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Returns the sum of all integer elements in given array
	def sumElement(self, arr, size) :
		#  Define resultant variable with default zero value
		result = 0
		#  Execute Loop through of array size
		i = 0
		while (i < size) :
			#  Add array elements
			result += arr[i]
			i += 1
		
		return result
	

def main() :
	task = Addition()
	#  Define array of integer elements
	arr = [3, 1, -3, 4, 2, 7]
	#  Get the size
	size = len(arr)
	print(" Array Element ")
	task.printArray(arr, size)
	sum = task.sumElement(arr, size)
	print(" Sum of elements : ", sum )

if __name__ == "__main__": main()

Output

 Array Element
   3   1   -3   4   2   7
 Sum of elements :  14
#  Ruby Program
#  Sum of elements in an array

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

		print("\n")
	end

	#  Returns the sum of all integer elements in given array
	def sumElement(arr, size) 
		#  Define resultant variable with default zero value
		result = 0
		#  Execute Loop through of array size
		i = 0
		while (i < size) 
			#  Add array elements
			result += arr[i]
			i += 1
		end

		return result
	end

end

def main() 
	task = Addition.new()
	#  Define array of integer elements
	arr = [3, 1, -3, 4, 2, 7]
	#  Get the size
	size = arr.length
	print(" Array Element \n")
	task.printArray(arr, size)
	sum = task.sumElement(arr, size)
	print(" Sum of elements : ", sum ,"\n")
end

main()

Output

 Array Element 
  3  1  -3  4  2  7
 Sum of elements : 14
/*
    Scala Program
    Sum of elements in an array
*/
class Addition
{
	//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;
		}
		print("\n");
	}
	// Returns the sum of all integer elements in given array
	def sumElement(arr: Array[Int], size: Int): Int = {
		// Define resultant variable with default zero value
		var result: Int = 0;
		// Execute Loop through of array size
		var i: Int = 0;
		while (i < size)
		{
			// Add array elements
			result += arr(i);
			i += 1;
		}
		return result;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Addition = new Addition();
		// Define array of integer elements
		var arr: Array[Int] = Array(3, 1, -3, 4, 2, 7);
		// Get the size
		var size: Int = arr.length;
		print(" Array Element \n");
		task.printArray(arr, size);
		var sum: Int = task.sumElement(arr, size);
		print(" Sum of elements : " + sum + "\n");
	}
}

Output

 Array Element
  3  1  -3  4  2  7
 Sum of elements : 14
/*
    Swift 4 Program
    Sum of elements in an array
*/
class Addition
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Returns the sum of all integer elements in given array
	func sumElement(_ arr: [Int], _ size: Int)->Int
	{
		// Define resultant variable with default zero value
		var result: Int = 0;
		// Execute Loop through of array size
		var i: Int = 0;
		while (i < size)
		{
			// Add array elements
			result += arr[i];
			i += 1;
		}
		return result;
	}
}
func main()
{
	let task: Addition = Addition();
	// Define array of integer elements
	let arr: [Int] = [3, 1, -3, 4, 2, 7];
	// Get the size
	let size: Int = arr.count;
	print(" Array Element ");
	task.printArray(arr, size);
	let sum: Int = task.sumElement(arr, size);
	print(" Sum of elements : ", sum );
}
main();

Output

 Array Element
   3   1   -3   4   2   7
 Sum of elements :  14
/*
    Kotlin Program
    Sum of elements in an array
*/
class Addition
{
	//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;
		}
		print("\n");
	}
	// Returns the sum of all integer elements in given array
	fun sumElement(arr: Array<Int>, size: Int): Int
	{
		// Define resultant variable with default zero value
		var result: Int = 0;
		// Execute Loop through of array size
		var i: Int = 0;
		while (i<size)
		{
			// Add array elements
			result += arr[i];
			i += 1;
		}
		return result;
	}
}
fun main(args: Array<String>): Unit
{
	var task: Addition = Addition();
	// Define array of integer elements
	var arr: Array<Int> = arrayOf(3, 1, -3, 4, 2, 7);
	// Get the size
	var size: Int = arr.count();
	print(" Array Element \n");
	task.printArray(arr, size);
	var sum: Int = task.sumElement(arr, size);
	print(" Sum of elements : " + sum + "\n");
}

Output

 Array Element
  3  1  -3  4  2  7
 Sum of elements : 14

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.

New Comment