Golang Program to check if two numbers are Amicable Numbers

Go ProgrammingServer Side ProgrammingProgramming

Steps

  • Read two integers and store them in separate variables.
  • Find the sum of the proper divisors of both the numbers.
  • Check if the sum of the proper divisors is equal to the opposite numbers.
  • If they are equal, they are amicable numbers.
  • Print the final result.
Enter number 1: 220
Enter number 2: 284
Amicable!
Enter number 1: 349
Enter number 2: 234
Not Amicable!

Example

 Live Demo

package main
import "fmt"
func main(){
   var a, b int
   fmt.Print("Enter first number: ")
   fmt.Scanf("%d", &a)
   fmt.Print("Enter second number: ")
   fmt.Scanf("%d", &b)
   sum1 := 0
   for i:=1; i<a; i++{
      if a%i==0{
         sum1+=i
      }
   }
   sum2 := 0
   for i:=1; i<b; i++{
      if b%i==0{
         sum2+=i
      }
   }
   if sum1==b && sum2==a{
      fmt.Println("Amicable!")
   } else{
      fmt.Println("Not Amicable!")
   }
}

Output

Enter first number: 220
Enter second number: 284
Amicable!
raja
Published on 31-Jul-2021 15:36:05
Advertisements