Friday, 11 October 2013

Amicable Numbers Program

Amicable number are pairs of numbers each of whose divisors add to the other
number


Example 
------------
import java.util.Scanner;
class Amicable
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int i,j,n,sum_i=0,sum_j=0; 
System.out.println("Enter 2 Numbers");
i=s.nextInt();
j=s.nextInt();
for(n=1;n<i;n++)
{
if(i%n==0)
sum_i += n;
}
for(n=1;n<j;n++)
{
if(j%n==0)
sum_j += n;
}
if( (sum_j == i)&& (sum_i==j))
System.out.println("Amicable");
else
System.out.println("Not Amicable");
}
}


 

No comments:

Post a Comment