Introduction
Finding the sum of elements in an array is a common task in programming. This involves iterating through the array and adding each element to a cumulative total. This guide will demonstrate how to write a Go program that calculates the sum of all elements in an array.
Problem Statement
Create a Go program that:
- Prompts the user to enter the number of elements in an array.
- Takes input for the elements of the array.
- Calculates and displays the sum of the elements.
Example:
- Input: Array elements: [1, 2, 3, 4, 5]
- Output: Sum of elements: 15
Solution Steps
- Import the fmt Package: Use import "fmt"to include the fmt package for formatted I/O operations.
- Write the Main Function: Define the mainfunction, which is the entry point of every Go program.
- Input the Number of Elements: Use fmt.Scanlnto take input from the user for the number of elements in the array.
- Input the Array Elements: Use a loop to input the elements of the array from the user.
- Calculate the Sum: Use a loop to iterate through the array and sum the elements.
- Display the Sum: Use fmt.Printlnto display the calculated sum.
Go Program
package main
import "fmt"
/**
 * Go Program to Find the Sum of Elements in an Array
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare a variable to hold the number of elements in the array
    var n int
    // Step 2: Prompt the user to enter the number of elements
    fmt.Print("Enter the number of elements in the array: ")
    fmt.Scanln(&n)
    // Step 3: Declare an array to hold the elements and a variable to hold the sum
    array := make([]int, n)
    var sum int
    // Step 4: Input the elements of the array
    fmt.Println("Enter the elements of the array:")
    for i := 0; i < n; i++ {
        fmt.Scanln(&array[i])
    }
    // Step 5: Calculate the sum of the elements
    for _, value := range array {
        sum += value
    }
    // Step 6: Display the sum of the elements
    fmt.Println("Sum of elements:", sum)
}
Explanation
Step 1: Declare Variables
- The variable nis declared to store the number of elements in the array.
Step 2: Input the Number of Elements
- The program prompts the user to enter the number of elements using fmt.Print. Thefmt.Scanlnfunction reads the input and stores it in thenvariable.
Step 3: Declare the Array and Sum Variable
- An array arrayis created usingmaketo holdninteger elements. The variablesumis initialized to0to store the sum of the array elements.
Step 4: Input the Array Elements
- The program uses a forloop to prompt the user to enter each element of the array, storing the input in the corresponding index ofarray.
Step 5: Calculate the Sum
- The program uses a forloop with therangekeyword to iterate over the array, adding each element tosum.
Step 6: Display the Sum
- The calculated sum of the array elements is displayed using fmt.Println.
Output Example
Example 1:
Enter the number of elements in the array: 5
Enter the elements of the array:
1
2
3
4
5
Sum of elements: 15
Example 2:
Enter the number of elements in the array: 3
Enter the elements of the array:
10
20
30
Sum of elements: 60
Conclusion
This Go program demonstrates how to calculate the sum of elements in an array by iterating through the array and summing each element. It covers basic programming concepts such as arrays, loops, and input/output operations. This example is useful for beginners learning Go programming and understanding how to work with arrays and perform arithmetic operations on their elements.