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 Odd Numbers

by suresh

This Go program to find the Sum of Odd Numbers from 1 to n uses the for loop to iterate between 1 and n. Inside the loop, if statement filters the odd numbers and adds total to odd total.

package main

import "fmt"

func main() {

    var oddnum, Oddtotal int

    fmt.Print("Enter the Number to find Odd Sum = ")
    fmt.Scanln(&oddnum)

    Oddtotal = 0
    fmt.Println("List of Odd Numbers from 1 to ", oddnum, " are = ")
    for x := 1; x <= oddnum; x++ {
        if x%2 != 0 {
            fmt.Print(x, "\t")
            Oddtotal = Oddtotal + x
        }
    }
    fmt.Println("\nSum of Odd Numbers from 1 to ", oddnum, " = ", Oddtotal)
}
Go Program to Find Sum of Odd Numbers 1

Golang Program to find Sum of Odd Numbers.

In this Golang program, we altered the for loop to start from 1 and increment by 2 to calculate the sum of odd numbers.

package main

import "fmt"

func main() {

    var oddnum, Oddtotal int

    fmt.Print("Enter the Number to find Odd Sum = ")
    fmt.Scanln(&oddnum)

    Oddtotal = 0
    fmt.Println("List of Odd Numbers from 1 to ", oddnum, " are = ")
    for x := 1; x <= oddnum; x = x + 2 {
        fmt.Print(x, "\t")
        Oddtotal = Oddtotal + x
    }
    fmt.Println("\nSum of Odd Numbers from 1 to ", oddnum, " = ", Oddtotal)
}
Go Program to Find Sum of Odd Numbers 2

This Go program calculates the sum of odd numbers from min to max. Here, we used an extra if statement to hold the user-entered value as odd by increment the even value (if any).

package main

import "fmt"

func main() {

    var Oddmin, Oddmax, Oddtotal int

    fmt.Print("Enter the Minimum Odd Number = ")
    fmt.Scanln(&Oddmin)
    fmt.Print("Enter the Maximum Odd Number = ")
    fmt.Scanln(&Oddmax)

    if Oddmin%2 == 0 {
        Oddmin++
    }

    Oddtotal = 0
    fmt.Println("Odd Numbers from ", Oddmin, " to ", Oddmax, " = ")
    for x := Oddmin; x <= Oddmax; x = x + 2 {
        fmt.Print(x, "\t")
        Oddtotal = Oddtotal + x
    }
    fmt.Println("\nSum of Odd Numbers from ", Oddmin, " to ", Oddmax, " = ", Oddtotal)
}
Go Program to Find Sum of Odd Numbers 3

It is identical to the first sum of odd Numbers example. However, it calculates the odd sum between the minimum and maximum limit.

package main

import "fmt"

func main() {

    var Oddmin, Oddmax, Oddtotal int

    fmt.Print("Enter the Minimum Odd Number = ")
    fmt.Scanln(&Oddmin)
    fmt.Print("Enter the Maximum Odd Number = ")
    fmt.Scanln(&Oddmax)

    Oddtotal = 0
    fmt.Println("Odd Numbers from ", Oddmin, " to ", Oddmax, " = ")
    for x := Oddmin; x <= Oddmax; x++ {
        if x%2 != 0 {
            fmt.Print(x, "\t")
            Oddtotal = Oddtotal + x
        }
    }
    fmt.Println("\nSum of Odd Numbers from ", Oddmin, " to ", Oddmax, " = ", Oddtotal)
}
Golang Program to Find Sum of Odd Numbers 4

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