Golang Program to Check if a Number is a Perfect Number



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!
Updated on: 2021-07-31T15:49:58+05:30

592 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements