Golang Program to check if two numbers are Amicable Numbers

Go ProgrammingServer Side ProgrammingProgramming

<h2>Steps</h2><ul class="list"><li>Read two integers and store them in separate variables.</li><li>Find the sum of the proper divisors of both the numbers.</li><li>Check if the sum of the proper divisors is equal to the opposite numbers.</li><li>If they are equal, they are amicable numbers.</li><li>Print the final result.</li></ul><table class="table table-bordered"><tbody><tr><td>Enter number 1: 220<br>Enter number 2: 284<br>Amicable!<br></td><td>Enter number 1: 349<br>Enter number 2: 234<br>Not Amicable!<br></td></tr></tbody></table><h2>Example</h2><p><a class="demo" href="http://tpcg.io/mbhJhxTh" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">package main import &quot;fmt&quot; func main(){ &nbsp; &nbsp;var a, b int &nbsp; &nbsp;fmt.Print(&quot;Enter first number: &quot;) &nbsp; &nbsp;fmt.Scanf(&quot;%d&quot;, &amp;a) &nbsp; &nbsp;fmt.Print(&quot;Enter second number: &quot;) &nbsp; &nbsp;fmt.Scanf(&quot;%d&quot;, &amp;b) &nbsp; &nbsp;sum1 := 0 &nbsp; &nbsp;for i:=1; i&lt;a; i++{ &nbsp; &nbsp; &nbsp; if a%i==0{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum1+=i &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;sum2 := 0 &nbsp; &nbsp;for i:=1; i&lt;b; i++{ &nbsp; &nbsp; &nbsp; if b%i==0{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum2+=i &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;if sum1==b &amp;&amp; sum2==a{ &nbsp; &nbsp; &nbsp; fmt.Println(&quot;Amicable!&quot;) &nbsp; &nbsp;} else{ &nbsp; &nbsp; &nbsp; fmt.Println(&quot;Not Amicable!&quot;) &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">Enter first number: 220 Enter second number: 284 Amicable!</pre>
raja
Updated on 31-Jul-2021 16:02:41

Advertisements