Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Go Program to find Sum of Array Items

by suresh

Write a Go program to find the sum of all the array items using For loop. Here, we used for loop to iterate array items from index position zero to len(numarray). Within the loop, we are adding each array item to sum.

package main

import "fmt"

func main() {

    numarray := []int{10, 20, 30, 40, 50}

    arrSum := 0

    for i := 0; i < len(numarray); i++ {
        arrSum = arrSum + numarray[i]
    }
    fmt.Println("The Sum of Array Items = ", arrSum)
}
Go Program to Find Sum of Array Items 1

Golang Program to find Sum of Array Items

This Go examples uses the for loop with range.

package main

import "fmt"

func main() {

    numarray := []int{15, 25, 35, 45, 55, 65, 75}

    arrSum := 0

    for _, a := range numarray {
        arrSum = arrSum + a
    }
    fmt.Println("The Sum of Array Items = ", arrSum)
}
Go Program to Find Sum of Array Items 2

This Go program allows user to enter the array size and array elements. Next, it prints the sum of those array elements.

package main

import "fmt"

func main() {

    var size int

    fmt.Print("Enter the Array Size = ")
    fmt.Scan(&size)

    numarray := make([]int, size)

    fmt.Print("Enter the Array Items = ")
    for i := 0; i < size; i++ {
        fmt.Scan(&numarray[i])
    }

    arrSum := 0

    for _, a := range numarray {
        arrSum = arrSum + a
    }
    fmt.Println("The Sum of Array Items = ", arrSum)
}
Go Program to Find Sum of Array Items 3

Placed Under: Go Examples

  • Golang Hello World Program
  • Go Add Two Number
  • Go Compound Interest
  • Go Count Digits in a Number
  • Go Cube of a Number
  • Go Even or Odd
  • Go Largest of Three Numbers
  • Go Reverse Natural Numbers
  • Go Multiplication Table
  • Go Number divisible by 5 and 11
  • Go Positive or Negative
  • Go Power of a Number
  • Go Print Natural Numbers
  • Go Profit or Loss
  • Go Print 1 to 100 without loop
  • Go Program to Print 1 to 100
  • Go Product of Digits in a Number
  • Go Palindrome Number Program
  • Go Print Even Numbers
  • Go Print Odd Numbers
  • Go Factors of a Number
  • Go Perfect Number
  • Go Prime Number
  • Go Reverse a Number
  • Go Simple Interest
  • Go Square of a Number
  • Go Square root of a Number
  • Go Sum of Digits in a Number
  • Go Sum & Avg of Natural Nums
  • Go Sum of Even Numbers
  • Go Sum of Odd Numbers
  • Go Sum of Even and Odd
  • Go Sphere Vol & Surface Area
  • Go Cylinder Vol & Surface Area
  • Go Cuboid Vol & Surface Area
  • Go Cube Volume & Surface Area
  • Go Cone Volume & Surface Area

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy