- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Golang program to calculate the sum of elements in a given array
Examples
- Input arr = [1, 3, 4, 5, 6] => 1+3+4+5+6 = 19
- Input arr = [5, 7, 8, 9, 1, 0, 6] => 5+7+8+9+1+0+6 = 36
Approach to solve this problem
- Step 1: Define a function that accepts an array.
- Step 2: Declare a variable, res = 0.
- Step 3: Iterate the array and add elements to res.
- Step 4: Return res.
Program
package main import "fmt" func findArraySum(arr []int) int{ res := 0 for i:=0; i<len(arr); i++ { res += arr[i] } return res } func main(){ fmt.Println(findArraySum([]int{1, 2, 3, 4, 5})) fmt.Println(findArraySum([]int{3, 5, 7, 2, 1})) fmt.Println(findArraySum([]int{9, 8, 6, 1, 0})) }
Output
15 18 24
- Related Articles
- Swift Program to Calculate the sum of Elements in a Given Array
- Write a Golang program to find duplicate elements in a given array
- Golang program to calculate the sum of right diagonal elements
- Golang Program to calculate the sum of columns of matrix elements
- Golang Program to calculate the sum of rows of matrix elements
- Write a Golang program to find pairs with the given sum in an array(O(n2))
- Write a Golang program to find pairs with the given sum in an array(O(n))
- Write a Golang program to find duplicate elements in a given range
- Write a Golang program to find pairs with given sum in an array(O(nlogn))
- Write a Golang program to find the sum of digits for a given number
- Golang Program to find the odd-occurring elements in a given array
- Program to find sum of elements in a given array in C++
- C/C++ Program to find the sum of elements in a given array
- Golang Program to Calculate the Average of Numbers in a Given List
- Write a Golang program to reverse an array

Advertisements