Go Program to Find the Sum of Elements in an Array

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

  1. Import the fmt Package: Use import "fmt" to include the fmt package for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Input the Number of Elements: Use fmt.Scanln to take input from the user for the number of elements in the array.
  4. Input the Array Elements: Use a loop to input the elements of the array from the user.
  5. Calculate the Sum: Use a loop to iterate through the array and sum the elements.
  6. Display the Sum: Use fmt.Println to 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 n is 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. The fmt.Scanln function reads the input and stores it in the n variable.

Step 3: Declare the Array and Sum Variable

  • An array array is created using make to hold n integer elements. The variable sum is initialized to 0 to store the sum of the array elements.

Step 4: Input the Array Elements

  • The program uses a for loop to prompt the user to enter each element of the array, storing the input in the corresponding index of array.

Step 5: Calculate the Sum

  • The program uses a for loop with the range keyword to iterate over the array, adding each element to sum.

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top