Go Program to check a number is a perfect number or not

In this program, You will learn how to check a number is a perfect number or not in Go.


if n%i == 0 {
    s = s + i
}

Some list of Perfect Numbers

Example: How to check a number is a perfect number or not in Go

package main

import "fmt"

func main() {

	var n int
	fmt.Print("Enter a number:")
	fmt.Scan(&n)

	s := 0
	for i := 1; i < n; i++ {
		if n%i == 0 {
			s = s + i
		}
	}

	if s == n {
		fmt.Println("It's a perfect number:", n)
	} else {
		fmt.Println("It's not a perfect number:", n)
	}

}

Output:

Enter a number:28
It's a perfect number: 28