Advertisements
Advertisements
Question
WAmicable Number Definition: The number n is amicable if it belongs to an amicable pair. Two numbers n and m are called an amicable pair if the sum of all positive divisors of n and the sum of all positive divisors of m are equal to (n + m).
First ten such numbers: 220, 284, 1184, 1210, 2620, 2924, 5020, 5564, 6232, 6368. Declare a class named Numbers contain a method AmicableNos(int, int) that will check both the numbers are of Amicable pairs or not. Appropriate error message should be given in case.
Code Writing
Advertisements
Solution
import java.io.*;
class Amicable2
{
public static void AmicableNos(int m, int n)
{
int i,f1=0,f2=0;
for(i=1;i<m;i++)
{
if(m%i= =0)
f1+=i;
}
for(i=l<n;i++)
{
if(n%i==0)
f2+=i;
}
f((f1+f2)= =(m+n))
System.out.println("Amicable pair:"+m+","+n);
else
System.out.println("Both are not amicable pair");
}
public static void main()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 2 numbers");
int m=Integer.parseInt(br.readLine());
int n=Integer.parseInt(br.readLine());
AmicableNos(m,n);
}
}shaalaa.com
Is there an error in this question or solution?
