Program to find sum of elements in a given array
Given an array of integers, find sum of its elements.
Examples :
Input : arr[] = {1, 2, 3} Output : 6 1 + 2 + 3 = 6 Input : arr[] = {15, 12, 13, 10} Output : 50
C++
/* C++ Program to find sum of elements in a given array */ #include <bits/stdc++.h> using namespace std; // function to return sum of elements // in an array of size n int sum( int arr[], int n) { int sum = 0; // initialize sum // Iterate through all elements // and add them to sum for ( int i = 0; i < n; i++) sum += arr[i]; return sum; } // Driver code int main() { int arr[] = {12, 3, 4, 15}; int n = sizeof (arr) / sizeof (arr[0]); cout << "Sum of given array is " << sum(arr, n); return 0; } // This code is contributed by rathbhupendra |
chevron_right
filter_none
C
/* C Program to find sum of elements in a given array */ #include <bits/stdc++.h> // function to return sum of elements // in an array of size n int sum( int arr[], int n) { int sum = 0; // initialize sum // Iterate through all elements // and add them to sum for ( int i = 0; i < n; i++) sum += arr[i]; return sum; } int main() { int arr[] = {12, 3, 4, 15}; int n = sizeof (arr) / sizeof (arr[0]); printf ( "Sum of given array is %d" , sum(arr, n)); return 0; } |
chevron_right
filter_none
Java
/* Java Program to find sum of elements in a given array */ class Test { static int arr[] = { 12 , 3 , 4 , 15 }; // method for sum of elements in an array static int sum() { int sum = 0 ; // initialize sum int i; // Iterate through all elements and add them to sum for (i = 0 ; i < arr.length; i++) sum += arr[i]; return sum; } // Driver method public static void main(String[] args) { System.out.println( "Sum of given array is " + sum()); } } |
chevron_right
filter_none
Python3
# Python 3 code to find sum # of elements in given array def _sum(arr,n): # return sum using sum # inbuilt sum() function return ( sum (arr)) # driver function arr = [] # input values to list arr = [ 12 , 3 , 4 , 15 ] # calculating length of array n = len (arr) ans = _sum(arr,n) # display sum print ( 'Sum of the array is ' ,ans) # This code is contributed by Himanshu Ranjan |
chevron_right
filter_none
C#
// C# Program to find sum of elements in a // given array using System; class GFG { // method for sum of elements in an array static int sum( int []arr, int n) { int sum = 0; // initialize sum // Iterate through all elements and // add them to sum for ( int i = 0; i < n; i++) sum += arr[i]; return sum; } // Driver method public static void Main() { int []arr = {12,3,4,15}; int n = arr.Length; Console.Write( "Sum of given array is " + sum(arr, n)); } } // This code is contributed by Sam007. |
chevron_right
filter_none
PHP
<?php // PHP Program to find sum of // elements in a given array // function to return sum // of elements in an array // of size n function sum( $arr , $n ) { // initialize sum $sum = 0; // Iterate through all elements // and add them to sum for ( $i = 0; $i < $n ; $i ++) $sum += $arr [ $i ]; return $sum ; } // Driver Code $arr = array (12, 3, 4, 15); $n = sizeof( $arr ); echo "Sum of given array is " , sum( $arr , $n ); // This code is contributed by aj_36 ?> |
chevron_right
filter_none
Output :
Sum of given array is 34
Recommended Posts:
- Find original array from encrypted array (An array of sums of other elements)
- Find elements larger than half of the elements in an array
- Find Kth element in an array containing odd elements first and then even elements
- Find all elements in array which have at-least two greater elements
- Find elements of array using XOR of consecutive elements
- Find minimum value to assign all array elements so that array product becomes greater
- Find element in array that divides all array elements
- Program to print Sum of even and odd elements in an array
- Program for multiplication of array elements
- Program to find the smallest element among three elements
- Python program to find sum of elements in list
- Program to print product of even and odd indexed elements in an Array
- Find four elements a, b, c and d in an array such that a+b = c+d
- Find the largest three elements in an array
- Find GCD of factorial of elements of given array