Tsallaka zuwa abun ciki
Justice Tech School

Simple Programs: Sum and Factorial

Combine loops and functions to solve small problems.

Example
package main

import "fmt"

func fact(n int) int {
	if n <= 1 {
		return 1
	}
	return n * fact(n-1)
}

func main() {
	sum := 0
	for i := 1; i <= 10; i++ {
		sum += i
	}
	fmt.Println(sum, fact(5))
}

Result

55 120

Exercise

Accumulate.

sum  i

Quiz Test

What is fact(5)?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.