Go language program to print sum of n natural numbers

Views: 111   

Following program shows you how to print sum of n natural numbers.
In this program we get number from user and shows sum of n natural numbers using following formula
Sum of n natural numbers = input X (input + 1)/2

package main

import "fmt"

func main() {

  var input int
  var result int
  fmt.Println("Enter a number:")
  fmt.Scanf("%d", &input)
  result = input * (input + 1) / 2
  fmt.Println(result)
  }

Output:

Enter a number:
 22
253
On By