Golang Program to Check if a Number is a Perfect Number

Go ProgrammingServer Side ProgrammingProgramming

Steps

  • Read an integer and store it in a variable.
  • Initialize a variable to count the sum of the proper divisors to 0.
  • Use a for loop and an if statement to add the proper divisors of the integer to the sum variable.
  • Check if the sum of the proper divisors of the number is equal to the variable.
  • Print the final result.
Enter any number: 6
The number is a Perfect number!
Enter any number: 25
The number is not a Perfect number!

Example

 Live Demo

package main
import "fmt"
func main(){
   var n int
   fmt.Print("Enter any number: ")
   fmt.Scanf("%d", &n)
   sum1 := 0
   for i:=1; i<n;i++{
      if n % i ==0{
         sum1 += i
      }
   }
   if sum1 == n {
      print("The number is a Perfect number!")
   }else{
      print("The number is not a Perfect number!")
   }
}

Output

Enter any number: 6
The number is a Perfect number!
raja
Published on 31-Jul-2021 15:34:45
Advertisements